|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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 Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month Last edited by Scorpions4ever : January 25th, 2004 at 12:37 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Question about 'random' |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|