How would you create a random number generator thats limited to a
specific rang? say
1930 - 1950
Thanks
···
--
Posted via http://www.ruby-forum.com/.
How would you create a random number generator thats limited to a
specific rang? say
1930 - 1950
Thanks
--
Posted via http://www.ruby-forum.com/.
# 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
From: stanislaus_d@hotmail.com [mailto:stanislaus_d@hotmail.com]
------------------------------------------------------------------------
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? say1930 - 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)
--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html
How would you create a random number generator thats limited to a
specific rang? say1930 - 1950
Generate random number between 0 and 20 and add that to 1930.
Regards,
Rimantas
David Stanislaus wrote:
How would you create a random number generator thats limited to a
specific rang? say1930 - 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.)
--
Posted via http://www.ruby-forum.com/\.
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:
From: stanislaus_d@hotmail.com [mailto:stanislaus_d@hotmail.com]
# How would you create a random number generator thats limited to a
# specific rang? say 1930 - 1950botp@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. Flow Control - Learn to Program 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)
=> 1947kind regards -botp
Urgh, I know I really don't understand ruby at all.
--
Posted via http://www.ruby-forum.com/\.
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
On Jun 9, 9:35 pm, Tim Hunter <TimHun...@nc.rr.com> wrote:
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. Sox = 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)
====
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.
David Stanislaus wrote:
How would you create a random number generator thats limited to a
specific rang? say1930 - 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
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
Rob@AgileConsultingLLC.com
On Jun 10, 2008, at 10:13 AM, Dave Bass wrote:
http://pine.fm/LearnToProgram/?Chapter=06 the bottom Deaf Grandma.
--
Posted via http://www.ruby-forum.com/.
I think I have it, Thanks a bunch all of you, for helping me!!!

--
Posted via http://www.ruby-forum.com/.
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...
--
Posted via http://www.ruby-forum.com/.
But one more thing, just for reference what does the x value/thingy
stand for?
--
Posted via http://www.ruby-forum.com/.
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.
--
Posted via http://www.ruby-forum.com/.
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?
On Mon, Jun 9, 2008 at 9:57 PM, David Stanislaus <stanislaus_d@hotmail.com> wrote:
Ok, i guess I kind of understand it but why (20)? shouldn't it be 1950?
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Hi --
On Tue, 10 Jun 2008, David Stanislaus wrote:
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.'
endSomething 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!
David A. Black wrote:
Hi --
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.
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
Rob@AgileConsultingLLC.com
On Jun 10, 2008, at 1:02 PM, David Stanislaus wrote:
David A. Black wrote:
Hi --
On Tue, 10 Jun 2008, David Stanislaus wrote:
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.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
so how would you fix that?