Random Number Stuff

D

David Stanislaus

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

1930 - 1950

Thanks
 
T

Tim Hunter

David said:
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)
 
D

David Stanislaus

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



From: (e-mail address removed) [mailto:[email protected]]
# How would you create a random number generator thats limited to a
# 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.
 
D

David Stanislaus

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

Eric I.

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)

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 Ruby 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.
 
D

David Stanislaus

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

David Stanislaus

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


Something about wrong identifier.
 
D

David A. Black

Hi --

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


Something about wrong identifier.

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

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
 
R

Rimantas Liubertas

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
 
R

Rick DeNatale

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

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

Dave Bass

David said:
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.)
 
R

Rob Biedenharn

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)

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

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
D

David Stanislaus

David said:
Hi --



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

Rob Biedenharn

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?


Perhaps you could look closely at what David gave you and then make
your code look like that.

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


Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

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
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top