Wednesday 15 May 2013

ruby - How do I generate a sum of random numbers? -


If I have a number from 0 to 70, then I want to add five random numbers and random numbers. So be 140 and less 220 then. I want to do this as long as the number of five random numbers does not occur at 140..220, and then display those five random numbers. *

I have done so far:

  5.times {r = rand (0 .. 70); Puts "# {r}"}   

or:

  5.times {puts "# {[* ... ... 70] Sample} "}    

If you need a real number from the sum, then its Use liner:

  nums = [* 0 ... 70]. Sample (5) Unless (140..220) === (nums || []). If injected (: +)   

can be repeated, then use it instead:

  nums = 5times.collect {rand (0 .. 70)} till (140 ..220) === (nums || []). Injection (: +)   

Multiline edition of another, with a state error (cap tip sumual):

  points = [] Valid = 140..220 Not valid until === Points-indject (+) = [] 5. Times {points> lt; & Lt; Rand (0 .. 70)} and    

No comments:

Post a Comment