Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How do I get a random number between two random numbers?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Alex Untitled  
View profile  
 More options Nov 14, 9:54 pm
Newsgroups: comp.lang.ruby
From: Alex Untitled <somebodydc6...@gmail.com>
Date: Sat, 14 Nov 2009 21:54:26 -0500
Local: Sat, Nov 14 2009 9:54 pm
Subject: How do I get a random number between two random numbers?
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/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile  
 More options Nov 14, 10:00 pm
Newsgroups: comp.lang.ruby
From: Marnen Laibow-Koser <mar...@marnen.org>
Date: Sat, 14 Nov 2009 22:00:57 -0500
Local: Sat, Nov 14 2009 10:00 pm
Subject: Re: How do I get a random number between two random numbers?

Alex Untitled 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?

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,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Untitled  
View profile  
 More options Nov 14, 10:03 pm
Newsgroups: comp.lang.ruby
From: Alex Untitled <somebodydc6...@gmail.com>
Date: Sat, 14 Nov 2009 22:03:26 -0500
Local: Sat, Nov 14 2009 10:03 pm
Subject: Re: How do I get a random number between two random numbers?

So, would it be something like this?

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

Anyway, thank you!
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Arcieri  
View profile  
 More options Nov 14, 10:08 pm
Newsgroups: comp.lang.ruby
From: Tony Arcieri <t...@medioh.com>
Date: Sat, 14 Nov 2009 22:08:26 -0500
Local: Sat, Nov 14 2009 10:08 pm
Subject: Re: How do I get a random number between two random numbers?
[Note:  parts of this message were removed to make it a legal post.]

On Sat, Nov 14, 2009 at 7:54 PM, Alex Untitled <somebodydc6...@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?

first + rand(last + 1 - first)

--
Tony Arcieri
Medioh/Nagravision


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Untitled  
View profile  
 More options Nov 14, 10:09 pm
Newsgroups: comp.lang.ruby
From: Alex Untitled <somebodydc6...@gmail.com>
Date: Sat, 14 Nov 2009 22:09:45 -0500
Local: Sat, Nov 14 2009 10:09 pm
Subject: Re: How do I get a random number between two random numbers?
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.
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Untitled  
View profile  
 More options Nov 14, 10:10 pm
Newsgroups: comp.lang.ruby
From: Alex Untitled <somebodydc6...@gmail.com>
Date: Sat, 14 Nov 2009 22:10:32 -0500
Local: Sat, Nov 14 2009 10:10 pm
Subject: Re: How do I get a random number between two random numbers?

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

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Untitled  
View profile  
 More options Nov 14, 10:31 pm
Newsgroups: comp.lang.ruby
From: Alex Untitled <somebodydc6...@gmail.com>
Date: Sat, 14 Nov 2009 22:31:30 -0500
Local: Sat, Nov 14 2009 10:31 pm
Subject: Re: How do I get a random number between two random numbers?
I found the solution.

num1 = rand(1001)
num2 = 1001 + rand(1001)
number = num1 + rand(num2)
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Wilhelm  
View profile  
 More options Nov 15, 11:52 pm
Newsgroups: comp.lang.ruby
From: Steve Wilhelm <st...@studio831.com>
Date: Sun, 15 Nov 2009 23:52:56 -0500
Local: Sun, Nov 15 2009 11:52 pm
Subject: Re: How do I get a random number between two random numbers?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Cheek  
View profile  
 More options Nov 16, 1:21 am
Newsgroups: comp.lang.ruby
From: Josh Cheek <josh.ch...@gmail.com>
Date: Mon, 16 Nov 2009 01:21:24 -0500
Local: Mon, Nov 16 2009 1:21 am
Subject: Re: How do I get a random number between two random numbers?
[Note:  parts of this message were removed to make it a legal post.]

On Sat, Nov 14, 2009 at 8:54 PM, Alex Untitled <somebodydc6...@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}"


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Wilhelm  
View profile  
 More options Nov 16, 2:45 am
Newsgroups: comp.lang.ruby
From: Steve Wilhelm <st...@studio831.com>
Date: Mon, 16 Nov 2009 02:45:13 -0500
Local: Mon, Nov 16 2009 2:45 am
Subject: Re: How do I get a random number between two random numbers?

The problem with your solution is that low and high can be the same
number.
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Cheek  
View profile  
 More options Nov 16, 3:08 am
Newsgroups: comp.lang.ruby
From: Josh Cheek <josh.ch...@gmail.com>
Date: Mon, 16 Nov 2009 03:08:12 -0500
Local: Mon, Nov 16 2009 3:08 am
Subject: Re: How do I get a random number between two random numbers?
[Note:  parts of this message were removed to make it a legal post.]

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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Giampiero Zanchi  
View profile  
 More options Nov 16, 4:45 am
Newsgroups: comp.lang.ruby
From: Giampiero Zanchi <ci...@tin.it>
Date: Mon, 16 Nov 2009 04:45:35 -0500
Local: Mon, Nov 16 2009 4:45 am
Subject: Re: How do I get a random number between two random numbers?
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}"
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google