From: Reiichi Tyrael on 1 Mar 2010 14:21 I must create a little game; the one who play it must choose 2 numbers, the beginning and the end of a range, and the program generate a secret random number within that range, so the player must guess it! But the problem is...how do I generate a random number? I though at something like this: number_to_guess = rand(n1..n2) but it give me the error message "in `rand': can't convert Range into Integer (TypeError)" What can I do? Thanks! -- Posted via http://www.ruby-forum.com/.
From: David Springer on 1 Mar 2010 14:35 Reiichi Tyrael, Is the range inclusive OR exclusive? Should n1 OR n2 be reurned sometimes or never? > number_to_guess = rand(n1..n2) -- Posted via http://www.ruby-forum.com/.
From: Reiichi Tyrael on 1 Mar 2010 14:40 David Springer wrote: > Reiichi Tyrael, > > Is the range inclusive OR exclusive? > > Should n1 OR n2 be reurned sometimes or never? > >> number_to_guess = rand(n1..n2) Inclusive! -- Posted via http://www.ruby-forum.com/.
From: David Springer on 1 Mar 2010 14:55 Reiichi Tyrael wrote: > Inclusive! Okay, I'll assume that these are two different positive integers. Try this: number_to_guess = (n2 > n1) ? n1 + rand((n2-n1+1)) : n2 + rand((n1-n2+1)) -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 1 Mar 2010 15:32 On 01.03.2010 20:21, Reiichi Tyrael wrote: > I must create a little game; the one who play it must choose 2 numbers, > the beginning and the end of a range, and the program generate a secret > random number within that range, so the player must guess it! > But the problem is...how do I generate a random number? > I though at something like this: > > number_to_guess = rand(n1..n2) > > but it give me the error message "in `rand': can't convert Range into > Integer (TypeError)" > > What can I do? Thanks! rand only accepts an integer which will make it produce values in the range 0...n (exclusive). So you can do: irb(main):001:0> low, high = 12, 56 => [12, 56] irb(main):002:0> r = low + rand(high - low) => 54 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
|
Next
|
Last
Pages: 1 2 Prev: Multiple invocations of one thor task from another thor task Next: Music Theory (#229) |