Home > OS >  populate array with random numbers
populate array with random numbers

Time:08-18

I have the following array:

nums = [10, 20, 30, 40, 50]

and another array that must have 3 items:

arr = [1]

How can I get one or two items from nums array (random) to populate arr that must have 3 elements?

CodePudding user response:

Use Array.sample

arr << num.sample(2)
arr.flatten

CodePudding user response:

nums = [10, 20, 30, 40, 50]
arr = [1]
2.times { arr << nums.sample }

Instead of sample(2) it's better to use sample without argument I think

Because sample with argument returns array with not repeated elements (with not repeated indices to be more precise)

That is, it is more random :)

Read more

CodePudding user response:

You can shuffle the array and take the first three elements to ensure you don't have repeats.

arr = nums.shuffle.take(3)

If, for instance, you were dealing with a situation like a card game where you need to draw from a shuffled deck more than once, you might want to store the shuffled array, and then pop from it.

shuffled = arr.shuffle
arr = shuffled.pop(3)
# shuffled now only has the remaining two elements

CodePudding user response:

Here are two ways to obtain a statistically random sample (subject to the impossibility of computing a truly random number).


The first is to select three elements at random and accept it if it contains at most two distinct elements. If it contains three distinct elements reject it and try again.

def doit(arr)
  loop do
    a = Array.new(3) { arr.sample }
    return a if a.uniq.size <= 2
  end
end
nums = [10, 20, 30, 40, 50]
doit nums #=> [40, 40, 10]
doit nums #=> [20, 50, 20]
doit nums #=> [50, 50, 20]

The second way is to make a probability calculation. Firstly, I assume that nums contains unique elements. (That assumption is not a deal-breaker but it avoids the messiness of dealing with the more complex case.)

n = nums.size
  = 5

First compute the probability of drawing one distinct element (e.g., [30, 30, 30]).

P[N1] = (1/n)(1/n)
      = (1/5)(1/5)
      = 1/25
      = 0.04  

Note the first element drawn can be anything, then the second and third draws must match the first draw, the probability of each being 1/5.

Next compute the probability of drawing three distinct elements:

P[N3] = ((n-1)/n)*((n-2)/n)
      = (n-1)*(n-2)/n**2
      = 4*3/5**2
      = 12/25
      = 0.48

Again, the first element can be anything. The probability of the second element drawn being different is (n-1)/n and the probability of the third element drawn being different than the first two is (n-1)/n.

Lastly, if N2 is the event in which exactly two of the three elements drawn are unique we have

P[N1]   P[N2]   P[N3] = 1

so

P[N2] = 1 - P[N1] - P[N3]
      = 1 - 0.04 - 0.48
      = 0.48

We therefore may compute

P[N1|N1 or N2] = P[N1 and (N1 or N2)]/P[N1 or N2]
P[N1|N1 or N2] = P[N1]/P[N1 or N2]
               = 0.04/(0.04   0.48)
               = 0.04/0.52
               = 0.0769

Therefore,

P[N2|N1 or N2] = 1 - P[N1|N1 or N2]
               = 1 - 0.0769
               = 0.9231

We therefore may write the following to obtain a statistically random sample.

def doit(arr)
  if rand < 0.0769
    n = arr.sample
    [n, n, n]
  else
    n1 = arr.sample
    n2 = (arr - [n1]).sample
    [n1, n2, n2]
  end
end
doit nums #=> [40, 40, 40]
doit nums #=> [10, 20, 20]
doit nums #=> [10, 20, 20]
  •  Tags:  
  • ruby
  • Related