LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2004, 05:18 AM   #1
Creep
LQ Newbie
 
Registered: Dec 2002
Distribution: Gentoo & Debian
Posts: 25

Rep: Reputation: 15
Fast random number function


Hi, I just profiled my program using gprof, and found out that it spends as much as 60% of it's time in a function which returns a pseudo random number. Now I'm looking for something a lot faster than the regular rand()-approach. I found out about the Mersenne Twister algorithm, which supposedly is a great deal faster than the libc-functions. The main problem is that I don't have a clue on how to make it return a random integer between X and Y, which is crucial for my program. I'd like to be able to do GetRandomNumber(3, 15); to get a pseudo random number between 3 and 15, for example.

The Mersenne Twister is available here:
http://www.math.keio.ac.jp/matumoto/...02/mt19937ar.c

I'd be really grateful for any help possible, because it's a real pain watching 60% of your programs execution time being wasted on random numbers, when it does lots and lots of more important operations in between. Thank you.
 
Old 03-07-2004, 06:42 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Without even looking at the algorithm, if you have an integer and you mod it by (Y-X) and add X to it then you will always get a number between X and Y.

Something like this would do
rnum = (rnum % (y-x))+x;
 
Old 03-07-2004, 09:37 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Don't know if it's faster, but try using the kernel random generator.
You could make a function that reads random bytes from /dev/urandom (these bytes are provided by the kernel) and use jtshaw's method to get a number within a range from it.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
random number generation sailu_mvn Programming 4 03-10-2005 02:10 PM
Random Number Game tearinox Programming 4 08-20-2004 10:12 PM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration