How do I get a random number between two random numbers?

A

Alex Untitled

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?
 
M

Marnen Laibow-Koser

Alex said:
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?

Well, you know how to generate the first two random numbers, right?
Just take the difference between those two as the range in which to
generate the third. Simple.

Best,
 
A

Alex Untitled

Marnen said:
Well, you know how to generate the first two random numbers, right?
Just take the difference between those two as the range in which to
generate the third. Simple.

Best,

So, would it be something like this?

num1 = rand(101)
num2 = rand(101)
num3 = num1 + num2

Anyway, thank you!
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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?

first + rand(last + 1 - first)
 
A

Alex Untitled

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.
 
A

Alex Untitled

Alex said:
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!
 
A

Alex Untitled

I found the solution.

num1 = rand(1001)
num2 = 1001 + rand(1001)
number = num1 + rand(num2)
 
S

Steve Wilhelm

Alex said:
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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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?

# 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}"
 
S

Steve Wilhelm

Josh said:
# 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.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Josh said:
# 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.
In that case, the cardinality of the solution set is 1, perhaps not
desirable for a guessing game, but it is not stated that this should not
happen, and if no minimum range is specified, then it is subjective where
that line is drawn. ie if a solution set with cardinality 1 is not
appropriate, then what about a solution set with cardinality two? At what
point do we say the numbers are sufficiently far apart that they are
acceptable?

Perhaps a different approach is required altogether. Instead of giving the
maximum value the numbers can be, give the minimum and maximum difference
between the numbers. I don't see much value in a minimum number anyway,
choosing a random number between 90 and 100 doesn't seem to offer anything
over choosing a minimum number between 0 and 10.

While I think saying "give me an upper bound between 10 and 20, with a
target between 0 and that number" is a better approach, it is not what was
asked for, and I thought it would be better to answer the question as asked,
than to impose my own restrictions.
 
G

Giampiero Zanchi

ciao
a lot of improvements is possible;

range_size = 64
n_middle = rand(101) + range_size
n_decrease = rand(range_size)
n_left = n_middle - n_decrease
n_right = n_middle + (range_size - n_decrease - 1)
p "#{n_left} - #{n_middle} - #{n_right}"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top