D
David Stanislaus
How would you create a random number generator thats limited to a
specific rang? say
1930 - 1950
Thanks
specific rang? say
1930 - 1950
Thanks
David said:How would you create a random number generator thats limited to a
specific rang? say
1930 - 1950
Thanks
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
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)
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.
specific rang? say
1930 - 1950
Ok, i guess I kind of understand it but why (20)? shouldn't it be 1950?
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.)
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?
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.