The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Question about 'random'
Discuss Question about 'random' in the Python Programming forum on Dev Shed. Question about 'random' Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 24th, 2004, 07:23 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Question about 'random'
If I have multiple files in a single project using function randint, should I run seed() somewhere in each file? Or can I just run seed() once and have it seed all randint calls across all files?
This question may seem silly, but I remember in C++ that the more you call seed, the less random your numbers are! If this is the case in Python, I'd like to avoid that effect...
Thanks,
tps
|

January 24th, 2004, 07:50 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
I've used 'random' in alot of different applications and i have to say i've never once needed seed()  , all you really need is randint(), then let Python handle the rest
Code:
>>> import random
>>> random.randint(1, 10)
5
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
2
>>> random.randint(1, 10)
1
>>> random.randint(1, 10)
7
>>> random.randint(1, 10)
2
>>> random.randint(1, 10)
7
>>> random.randint(1, 10)
2
>>> random.randint(1, 10)
8
>>>
Let me know if you have any questions
Mark.
__________________
programming language development: www.netytan.com – Hula
|

January 24th, 2004, 11:50 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
seed() is for making reproducible results, really. Python uses the "Mersenne Twister" algorithm to produce random numbers, and while it is periodic, it has a HUGE period. In fact, I know the exact size: (2**19937)-1. So if you start at the same point, you actually will get the same set of values for each time after. Observe:
Code:
>>> import random
>>> random.randint(1,100)
36
>>> random.randint(1,100)
93
>>> random.seed(42)
>>> random.randint(1,100)
64
>>> random.randint(1,100)
3
>>> random.randint(1,100)
28
>>> random.seed(42)
>>> random.randint(1,100)
64
>>> random.randint(1,100)
3
>>> random.randint(1,100)
28
It's a way you can test and make sure you are getting the "expected" random value in automated tests, for example. But you never have to explicitly seed the generator, as it will automatically seed it with the system time, I believe.
|

January 25th, 2004, 12:27 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
The random generation module automatically seeds itself with the current system time, when you first import the module. Also, another interesting thing to note is that if you call random.seed() in your code with no arguments (or None), it will use the current system time as the seed.
[edit]Note that older versions of python (below 2.1) used Wichmann Hill instead of Mersenne Twister as the algorithm for the random module.[/edit]
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Last edited by Scorpions4ever : January 25th, 2004 at 12:37 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|