[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Random Number Stuff

David Stanislaus

6/10/2008 1:10:00 AM

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

1930 - 1950

Thanks
--
Posted via http://www.ruby-....

15 Answers

Tim Hunter

6/10/2008 1:35:00 AM

0

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)

--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

David Stanislaus

6/10/2008 1:44:00 AM

0

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 - 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/?... 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.
--
Posted via http://www.ruby-....

David Stanislaus

6/10/2008 1:47:00 AM

0

http://pine.fm/LearnToProgram/?... the bottom Deaf Grandma.
--
Posted via http://www.ruby-....

David Stanislaus

6/10/2008 1:58:00 AM

0

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

Eric I.

6/10/2008 2:37:00 AM

0

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. 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://Lea... for all the details.

David Stanislaus

6/10/2008 4:45:00 AM

0

I think I have it, Thanks a bunch all of you, for helping me!!!
:)
--
Posted via http://www.ruby-....

David Stanislaus

6/10/2008 4:45:00 AM

0

But one more thing, just for reference what does the x value/thingy
stand for?
--
Posted via http://www.ruby-....

David Stanislaus

6/10/2008 4:54:00 AM

0


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

David A. Black

6/10/2008 6:52:00 AM

0

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.'
> 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.r... for details and updates!

Rimantas Liubertas

6/10/2008 8:53:00 AM

0

> 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
--
http://rim...