SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old January 24th, 2004, 07:23 PM
theperfectsoup theperfectsoup is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 35 theperfectsoup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old January 24th, 2004, 07:50 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old January 24th, 2004, 11:50 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #4  
Old January 25th, 2004, 12:27 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,394 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 8 h 36 m 18 sec
Reputation Power: 715
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Question about 'random'


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway