Prev: Customized GUI
Next: bsearch.rb
From: Alex Untitled on 14 Nov 2009 22:10 Alex Untitled wrote: > OK, that didn't work. That just added the two numbers. How do I make a > number between two numbers again? I can't seem to find it. I did not see your reply when I typed this. Thanks! -- Posted via http://www.ruby-forum.com/.
From: Alex Untitled on 14 Nov 2009 22:31 I found the solution. num1 = rand(1001) num2 = 1001 + rand(1001) number = num1 + rand(num2) -- Posted via http://www.ruby-forum.com/.
From: Steve Wilhelm on 15 Nov 2009 23:52 Alex Untitled wrote: > I found the solution. > > num1 = rand(1001) > num2 = 1001 + rand(1001) > number = num1 + rand(num2) This can return: num1 = 1000, num2 = 1001, number = 2001 Try module RandomNumberBetweenTwoRandomNumbers def self.generate max first = 1 + rand(max - 2); second = max - rand(max - (first + 1)) between = first + 1 + rand((second - first) - 1) [first, between, second] end end puts RandomNumberBetweenTwoRandomNumbers::generate 10 puts RandomNumberBetweenTwoRandomNumbers::generate 100 puts RandomNumberBetweenTwoRandomNumbers::generate 1000 -- Posted via http://www.ruby-forum.com/.
From: Josh Cheek on 16 Nov 2009 01:21 [Note: parts of this message were removed to make it a legal post.] On Sat, Nov 14, 2009 at 8:54 PM, Alex Untitled <somebodydc691n(a)gmail.com>wrote: > I want to create a program that asks you to guess a number between two > numbers. The problem is that I can't figure out how to make the numbers > that you're guessing between random and have the random number you are > guessing between those two numbers. Does anybody know how to do this? > -- > Posted via http://www.ruby-forum.com/. > > # How about something like: max = 100 low , high = [ rand(max) , rand(max) ].sort difference = high - low middle = low + rand(difference).to_i #to_i for if low and high have same value puts "low = #{low}" puts "middle = #{middle}" puts "high = #{high}"
From: Steve Wilhelm on 16 Nov 2009 02:45
Josh Cheek wrote: > > # How about something like: > > max = 100 > > low , high = [ rand(max) , rand(max) ].sort > > difference = high - low > > middle = low + rand(difference).to_i #to_i for if low and high have > same > value > > puts "low = #{low}" > puts "middle = #{middle}" > puts "high = #{high}" The problem with your solution is that low and high can be the same number. -- Posted via http://www.ruby-forum.com/. |