[Tutor] random number equations . . .

Glen Wheeler gew75 at hotmail.com
Thu Jun 3 18:04:12 EDT 2004


  Well, as a suggestion, instead of using the global declaration you could
write a little function which returned a randum number in some range on
demand.

>>> def gimmeRandomInRange(rng):
..  return int(round(random.random()*rng))
..
>>> gimmeRandomInRange(100)
99
>>> for i in range(100):
..  print gimmeRandomInRange(1000)
..
910
664
466
186
600
930
528
65
392
..

  Of course you could just use the random.randint() function, but none of
these are as fun as writing your own ;).

  Glen

----- Original Message ----- 
From: Dragonfirebane at aol.com
To: gew75 at hotmail.com
Cc: tutor at python.org
Sent: Friday, June 04, 2004 8:00 AM
Subject: Re: [Tutor] random number equations . . .


I was thinking of using the random number generator to randomly choose a
number from the 10 numbers, but i think that would be a bit excessive.
However, i might need more random numbers later (hence {global rannum =[]}),
so i figure i might as well leave all ten there.

In a message dated 6/3/2004 5:58:31 PM Eastern Standard Time,
gew75 at hotmail.com writes:
  Yep.  Although, I'm not sure what you want to do with them, but that will
generate ten random numbers in your desired range.
  If you have any other questions, just send them along.

Glen



More information about the Tutor mailing list