472,328 Members | 1,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

random.jumpahead: How to jump ahead exactly N steps?

The random.jumpahead documentation says this:

Changed in version 2.3: Instead of jumping to a specific state, n steps
ahead, jumpahead(n) jumps to another state likely to be separated by
many steps..

I really want a way to get to the Nth value in a random series started
with a particular seed. Is there any way to quickly do what jumpahead
apparently used to do?

I devised this function, but I suspect it runs really slowly:

def trudgeforward(n):
'''Advance the random generator's state by n calls.'''
for _ in xrange(n): random.random()

So any speed tips would be very appreciated.

TIA
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Jun 21 '06 #1
2 3173
[Matthew Wilson]
The random.jumpahead documentation says this:

Changed in version 2.3: Instead of jumping to a specific state, n steps
ahead, jumpahead(n) jumps to another state likely to be separated by
many steps..

I really want a way to get to the Nth value in a random series started
with a particular seed. Is there any way to quickly do what jumpahead
apparently used to do?
No known way, and it seems unlikely that any quick way will be found
in my lifetime ;-) for the Mersenne Twister. In Pythons <= 2.2, the
underlying generator was the algebraically _very_ much simpler
original Wichman-Hill generator, and jumping ahead by exactly n steps
was just a matter of a few modular integer exponentiations. That took
time proportional to log(n), so was extremely fast.

It was also much more _necessary_ using W-H, since W-H's period was
only around 10**13, while the Twister's period is around 10**6001:
even if you're going to use a billion random numbers per sequence, the
Twister's period has a way-way-beyond astronomical number of
non-overlapping subsequences of that length. The chance of hitting an
overlap is correspondingly miniscule.
I devised this function, but I suspect it runs really slowly:
Well, it takes exactly as much time as it takes to call random() n times.
def trudgeforward(n):
'''Advance the random generator's state by n calls.'''
for _ in xrange(n): random.random()

So any speed tips would be very appreciated.


What are you trying to achieve in the end? Take it as a fact that
there is no known way to advance the Twister by n states faster than
what you've already found.
Jun 21 '06 #2
Matthew Wilson wrote:
The random.jumpahead documentation says this:

Changed in version 2.3: Instead of jumping to a specific state, n steps
ahead, jumpahead(n) jumps to another state likely to be separated by
many steps..
This change was necessary because the random module got a new default
generator in 2.3. The new generator uses the Mersenne Twister
algorithm. Pre 2.3, Wichmann-Hill was used. (For more details, search
for "jumpahead" in
http://www.python.org/download/releases/2.3/NEWS.txt)

Unlike WH, there isn't a way to directly compute the Nth number in the
sequence using MT. If you're curious as to why,
textbooks/journals/Google are your friends. :-)
I really want a way to get to the Nth value in a random series started
with a particular seed. Is there any way to quickly do what jumpahead
apparently used to do?
You can always use the old WH generator. It's still available:
import random
wh = random.WichmannHill()
N, SEED = 100, 0
wh.seed(SEED)
for i in range(N): dummy = wh.random()
wh.random() 0.68591619673484816 wh.seed(SEED)
wh.jumpahead(N)
wh.random()

0.68591619673484816
I devised this function, but I suspect it runs really slowly:
Don't just suspect. Experiment, too. :-)
def trudgeforward(n):
'''Advance the random generator's state by n calls.'''
for _ in xrange(n): random.random()

So any speed tips would be very appreciated.


Python's random generator is implemented in C and is quite fast. In my
tests, your trudgeforward performs acceptably with n<~100000.

"import psyco" usually worth a try when improving execution speed, but
it won't help you here. All the real work is being done in C; the
overhead of the Python interpreter is neglible.

Hope that helps,
--Ben

Jun 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new...
8
by: Danny Smith | last post by:
Hi, I need to read a file and be able to: 1. Find the current position in the stream 2. Have access to a handy ReadLine() method. Obviously...
3
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I...
21
by: Gary Bond | last post by:
Hi All, I am a bit stuck with a project: Specifically, when making a database like engine in 'the old days', I would have wrapped a record class...
4
by: Bill Jackson | last post by:
Is there a preferred random library? scipy.random random Besides scipy's library returning ndarrays, is there any other...
18
by: kittikun | last post by:
Hi all, I was wondering if there is a random generator that is "reversible". I need to go back in time in my application and still need to use...
11
by: Alex | last post by:
Hi everybody, I wonder if it is possible in python to produce random numbers according to a user defined distribution? Unfortunately the random...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.