better,random,number,generator,than,comes,sta
Quick Search for:  in language:    
better,random,number,generator,than,comes,sta
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 485,557 lines
 Jobs: 1,067 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
Cellular Automata Encryption
By nhsxth on 10/24


mini math programs
By Johno on 10/24


The matrix
By Alcodes on 10/24


Palindrome Finder
By Jonathan Volk on 10/24


processor identification and info
By Razvan Petrescu on 10/24


Login Interface using ncurses for Linux
By Suvoraj Biswas on 10/24


Matrix Encryption Algorithm
By Tony Fecteau on 10/23


SMALL TEXT GAME
By cJ! on 10/23


stod
By Mark Jundo P. Documento on 10/23


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

A better Random Number Generator

Print
Email
 
VB icon
Submitted on: 12/4/1999
By: Andy Williams  
Level: Intermediate
User Rating: By 6 Users
Compatibility:C++ (general)

Users have accessed this code 32879 times.
 

(About the author)
 
     This is a better random number generator than comes standard. It's from a very good book on algoritms. You should read it
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: A better Random Number Generato
//     r
// Description:This is a better random n
//     umber generator than comes standard. It'
//     s from a very good book on algoritms. Yo
//     u should read it
// By: Andy Williams
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/xq/ASP/txtCod
//     eId.239/lngWId.3/qx/vb/scripts/ShowCode.
//     htm//for details.//**************************************
//     

#include <iostream.h>
#include <time.h>
void init_mm( );
int number_range( int from, int to );
int number_mm( void );
static	int	rgiState[2+55]; // leave this alone
void main()
    {
    	init_mm(); //seed the number generator
    	int random = number_range( 10, 100 );
    	cout << random << endl;
}

int number_mm( void ) { int *piState; int iState1; int iState2; int iRand; piState = &rgiState;[2]; iState1 = piState[-2]; iState2 = piState[-1]; iRand = ( piState[iState1] + piState[iState2] ) & ( ( 1 << 30 ) - 1 ); piState[iState1] = iRand; if ( ++iState1 == 55 ) iState1 = 0; if ( ++iState2 == 55 ) iState2 = 0; piState[-2] = iState1; piState[-1] = iState2; return iRand >> 6; }
/* * Generate a random number. */ int number_range( int from, int to ) { int power; int number; if ( ( to = to - from + 1 ) <= 1 ) return from; for ( power = 2; power < to; power <<= 1 ) ; while ( ( number = number_mm( ) & ( power - 1 ) ) >= to ) ; return from + number; }
/* * this is the Mitchell-Moore algorithm from Knuth Volume II. */ void init_mm( ) { int *piState; int iState; piState = &rgiState;[2]; piState[-2] = 55 - 55; piState[-1] = 55 - 24; piState[0] = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 ); piState[1] = 1; for ( iState = 2; iState < 55; iState++ ) { piState[iState] = ( piState[iState-1] + piState[iState-2] ) & ( ( 1 << 30 ) - 1 ); }
return; }


Other 3 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
2/14/2000 9:49:18 AM:tony
briliant
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/2/2000 4:12:37 AM:Skuggi
Hmmm... this thing wont be compiled. 
the compiler gives me these two error 
messages:
Error E2268 random1.cpp 
33: Call to undefined function 'int_mm' 
in function main()
and
Error 
E2451 random1.cpp 45: Undefined symbol 
'rgiState' in function 
number_mm()
i'm using borland c++ 
5.5 compiler.
What is wrong??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/2/2000 9:54:34 AM:Lolindrath
Look closely at your errors, the 
function name is init_mm, not int_mm. 
init_mm initializes the random number 
generator. Double check that the 
rgiState[2+55]; looks like it does in 
my submission. It kind of looks like it 
got commented out or something.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/30/2000 12:14:44 PM:Leo Tokarski
Excellent!  Finally, a random 
number
generator where the range 
variables
aren't constants!  The 
single typo was
easily edited...just 
replace the "void
int_mm();" line in 
main() with "void
init_mm();".
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/24/2000 2:50:28 PM:Jonathan Barber
I am compiling this code on a non 
Borland compiler on a vax or unix 
terminal.  It is giving me an error 
with time(NULL), which is in init_mm(). 
 If you have an suggestions, let me 
know. 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/13/2001 1:00:01 PM:ejoeli@hotmail.com
great - may need to re-order the 
functions here but otherwise works just 
fine. may need to add #include 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/13/2001 1:01:25 PM:ejoeli@homail
may need to add #include
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/13/2001 1:02:37 PM:#include
ejoeli@hotmail.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/13/2001 1:03:20 PM:ejoeli@hotmail
time.h
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/23/2001 10:10:36 PM:Andy Williams
Ok guys, I fixed all the problems with 
that, I'm sorry I neglected it for so 
long. It should run rather well 
now.
Andy
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/25/2001 4:38:52 AM:Vitaly Belman
Something is wrong with this code:    
for(int i=0; i<10; 
i++)
{
init_mm(); //seed the number 
generator
int random = number_range( 
10, 100 );
cout << random << 
endl;
}
The following code 
generates 10 times the same number. 
What's wrong?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/16/2001 11:01:02 PM:Greg Toombs
Instead of using time(NULL) to 
initialize your random data, use 
QueryPerformanceCounter. It's much, 
much, much, MUCH more effective.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/1/2001 12:58:45 AM:Dustin Davis
Yeah if i put this a loop, i get the 
same number no matter how many times i 
run it, i got the same number, i am 
calling all three lines fo coed in the 
loop, should i not do that?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/1/2001 1:00:31 AM:Dustin Davis
Never mind, i got it working, do not 
put the init_mm in the loop, or it will 
get the same number over and over, call 
it once and make sure to set int random 
before you set its random value.
int 
random;
random = blah blah...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/1/2001 11:19:43 AM:Andrew Williams
You got it, if you keep re-seeding the 
random number then you'll keep getting 
the same number. I'm not sure why this 
happens though.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/17/2002 7:34:09 PM:Curtis Miller
Like it alot get a different number 
almost every time. It seems to work 
better than rando.h. Think it would be 
cool if you made it a class. I did that 
and i makes it easier to just call it.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/2/2002 12:43:31 PM:wynx
What is the function of the rgiState ? 
You wrote there leave it alone.. but 
what's the use of it ?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/2/2002 11:53:44 PM:wynx
I am just a begineer, so I am not clear 
about this. Why don't you use 
srand(time(NULL));
randomNum=rand();
to generate random number ? isn't it 
easier than using all the complex 
function? If you don't mind, can you 
explain what's the logic/ idea you used 
to generate the random number?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/28/2002 4:20:04 AM:dathui
I just find one bad thing with this 
code, it always generate the same 
number the whole second, if you try 
looping it, it will show the same 
number until a new second begin.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/17/2002 10:36:31 PM:Joseph
Nice code.
Would you please let us 
know which book was that? "very good 
book on algoritms"
Thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/8/2002 10:57:18 AM:
This is great, I had to add a line(or 
three) to see the results of the 
generator before it closed.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.