Full Question is:
Write a method to randomly generate a set of m integers from an array of size
n
. Each element must have equal probability of being chosen`
This question is picked from "Crack the coding interview" and the solution is this:
We can swap the element with an element at the beginning of the array and then “remember” that the array now only includes elements
j
and greater. That is, when we picksubset[0]
to bearray[k]
, we replacearray[k]
with the first element in the array. When we picksubset[1]
, we considerarray[0]
to be “dead” and we pick a random elementy
between 1 and arraysize()
. We then set subset[1] equal toarray[y]
, and setarray[y]
equal to array[1]. Elements 0 and 1 are now “dead”.Subset[2]
is now chosen fromarray[2]
througharray[array size()]
, and so on.
My question is that if we're shrinking the array from which we're picking random numbers then probability of each number being picked 1/remaining_num_elements
. How does it stay equal for all the elements?
In order to generate random array of integers in Java, we use the nextInt() method of the java. util. Random class. This returns the next random integer value from this random number generator sequence.
To create a random integer array from an interval using streams, the better option would be to do e.g. int[] array = new Random(). ints(100, 0, 100).
Think about it like you're picking m
random numbers from a bag of n
numbers, with the first j
elements representing the numbers in your hand
and the remaining elements representing the numbers still in the bag
. (You iterate j
from 0 to m - 1
to pull out numbers, as your book suggests. j
, then, represents the number of integers that you have already pulled out of the bag.)
If you're picking m
integers from a bag in real life, then every time you pick a new number, you take from the bag only and not from your hand. Thus, the remaining_num_elements
shrinks at each step.
When you think this way, it should be simple to see that this method guarantees that each element has equal probability of being chosen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With