# Random Number Stuff

How would you create a random number generator thats limited to a
specific rang? say

1930 - 1950

Thanks

# specific rang? say 1930 - 1950

## botp@botp-desktop:~\$ qri rand ------------------------------------------------------------ Kernel#rand rand(max=0) => number

`````` Converts max to an integer using max1 = max.to_i.abs. If the
result is zero, returns a pseudorandom floating point number
greater than or equal to 0.0 and less than 1.0. Otherwise, returns
a pseudorandom integer greater than or equal to zero and less than
max1. Kernel::srand may be used to ensure repeatable sequences of
random numbers between different runs of the program. Ruby
currently uses a modified Mersenne Twister with a period of
2**19937-1.
``````

so,

irb(main):007:0> 1930 + rand(1950 - 1930)
=> 1940
irb(main):008:0> 1930 + rand(1950 - 1930)
=> 1932
irb(main):009:0> 1930 + rand(1950 - 1930)
=> 1947

kind regards -botp

David Stanislaus wrote:

How would you create a random number generator thats limited to a
specific rang? say

1930 - 1950

Thanks

Observe that Kernel#rand accepts an argument. If present and not 0, then
rand returns numbers >= 0.0 and < the argument. So

x = rand(20)

will always return a number n such that 0.0 <= n < 20. All you have to
do is add 1930 to it:

x = 1930 + rand(20)

Oh man…
It’s probably faster to show you what I do understand, but I’m just
goint to show you what I don’t…

PeÃ±a, Botp wrote:

# specific rang? say 1930 - 1950

## botp@botp-desktop:~\$ qri rand ------------------------------------------------------------ Kernel#rand rand(max=0) => number

`````` Converts max to an integer using max1 = max.to_i.abs. If the
result is zero, returns a pseudorandom floating point number
greater than or equal to 0.0 and less than 1.0. Otherwise, returns
a pseudorandom integer greater than or equal to zero and less than
max1. Kernel::srand may be used to ensure repeatable sequences of
random numbers between different runs of the program. Ruby
currently uses a modified Mersenne Twister with a period of
2**19937-1.
``````

[none of this but I don’t think it is really necessary for my question so we can just disregard it]
so,
[whats irb whats all this main stuff and things, why is 1930 out side and added to the rand couldn’t I just use rand(1950 - 1930)??? and maybe if I showed you what I was trying to do. http://pine.fm/LearnToProgram/?Chapter=06 the bottom Deaf Grandma.]
irb(main):007:0> 1930 + rand(1950 - 1930)
=> 1940
irb(main):008:0> 1930 + rand(1950 - 1930)
=> 1932
irb(main):009:0> 1930 + rand(1950 - 1930)
=> 1947

kind regards -botp

Urgh, I know I really don’t understand ruby at all.

Ok, i guess I kind of understand it but why (20)? shouldn’t it be 1950?

I know it has something to do with the range of 1930 and 1950 being 20
but…

On Jun 9, 9:35 pm, Tim H. [email protected] wrote:

x = rand(20)

will always return a number n such that 0.0 <= n < 20. All you have to
do is add 1930 to it:

x = 1930 + rand(20)

Well if the desired range is 1930-1950 inclusive, then it should be:

``````x = 1930 + rand(21)
``````

since there are 21 distinct values in the inclusive range.

Or, more generally for inclusive boundaries:

``````x = low + rand(high - low + 1)
``````

Eric

====

LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE
workshops.
Ruby Fundamentals Wkshp June 16-18 Ann Arbor, Mich.
Ready for Rails R. Wkshp June 23-24 Ann Arbor, Mich.
Ruby on Rails Wkshp June 25-27 Ann Arbor, Mich.
Ruby Plus Rails Combo Wkshp June 23-27 Ann Arbor, Mich
Please visit http://LearnRuby.com for all the details.

http://pine.fm/LearnToProgram/?Chapter=06 the bottom Deaf Grandma.

I think I have it, Thanks a bunch all of you, for helping me!!!

But one more thing, just for reference what does the x value/thingy
stand for?

puts “SPEAK up, Sonny, I can’t hear you.”
talk = gets.chomp
while command != ‘bye’
if talk == talk.upcase
puts ‘no not since’ x = 1930 + rand(21)
else
puts ‘What? Sonny, speak louder! Like THIS.’
end
end

How would you create a random number generator thats limited to a
specific rang? say

1930 - 1950

Generate random number between 0 and 20 and add that to 1930.

Regards,
Rimantas

On Mon, Jun 9, 2008 at 9:57 PM, David Stanislaus
[email protected]
wrote:

Ok, i guess I kind of understand it but why (20)? shouldn’t it be 1950?

rand(i) if i is nonzero, returns a number between 0 and i - 1.
(Actually i
converted to an integer - 1).

So rand(1950) will return a random number between 0 and 1949, which is
not
what you are looking for.

To return a random number between a and b inclusive, you need

a + rand(1+b-a)

so for a = 1930, b = 1950

1930 + rand(21)

which is the sum of 1930 and a random number between 0 and 20, which of
course will be a random number between 1930 and 1950.

Cappisch?

Rick DeNatale

My blog on Ruby

Hi –

On Tue, 10 Jun 2008, David Stanislaus wrote:

puts “no not since #{1930 + rand(21)}”

You don’t need to assign it to a variable (unless you’re going to use
it again), and hanging an assignment off the end of a puts statement
won’t work anyway – so you might as well just embed it in the string
you’re printing.

David

David Stanislaus wrote:

How would you create a random number generator thats limited to a
specific rang? say

1930 - 1950

If you want to do this a lot, you could add your own method to the
builtin Range class:

class Range
def random
self.begin + rand(self.end - self.begin + 1)
end
end

Then generate random numbers like this:

puts (1930…1950).random

(For some reason this gives “warning: (…) interpreted as grouped
expression”; I don’t know why. Maybe someone more experienced could
explain.)

On Jun 10, 2008, at 10:13 AM, Dave B. wrote:

def random
self.begin + rand(self.end - self.begin + 1)

end
end

It would be safer to do:
class Range
def random
case self.begin <=> self.end
when -1
self.begin + rand(self.end - self.begin +
(self.exclude_end? ? 0 : 1))
when 0
self.begin
when +1
self.begin - rand(self.begin - self.end +
(self.exclude_end? ? 0 : 1))
end
end
end

Since a Range does not require the endpoints to be ordered. And
1930…1950 is different from 1930…1950 according to whether 1950 is
to be included.

Then generate random numbers like this:

puts (1930…1950).random

(For some reason this gives “warning: (…) interpreted as grouped
expression”; I don’t know why. Maybe someone more experienced could
explain.)

Because your usage is to group the 1930…1950 so the random method is
sent to the Range object and not to the 1950. If you added the
parentheses for the puts, the opening parenthesis would no longer be
ambiguous:
puts((1930…1950).random)

tries = Hash.new {|h,k| h[k] = 0 }
range = 1930…1950
500.times {|_| tries[range.random] += 1 }
tries.sort.each {|year,count| puts “%d %3d”%[year,count] }

-Rob

David A. Black wrote:

Hi –

On Tue, 10 Jun 2008, David Stanislaus wrote:

puts “no not since #{1930 + rand(21)}”

You don’t need to assign it to a variable (unless you’re going to use
it again), and hanging an assignment off the end of a puts statement
won’t work anyway – so you might as well just embed it in the string
you’re printing.

puts “SPEAK up, Sonny, I can’t hear you.”
talk = gets.chomp
while command != ‘bye’
if talk == talk.upcase
puts ‘no not since’ 1930 + rand(21)
else
puts ‘What? Sonny, speak louder! Like THIS.’
end
end
so how would you fix that?

On Jun 10, 2008, at 1:02 PM, David Stanislaus wrote:

it again), and hanging an assignment off the end of a puts statement
end
end
so how would you fix that?

Perhaps you could look closely at what David gave you and then make

Then, you can look at the difference in Ruby between strings with
’ (single quote, aka apostrophe) and with " (double quote) with regard
to #{} interpolation.

-Rob