Solved

A simple RNG algorithm?

Posted on 2001-07-28
8
989 Views
Last Modified: 2012-08-14
Hi!

I want to learn how RNG:s work, but I have problems grasping the complex examples which I've found.

Could someone please supply an algorithm (preferrably in C or VB) for a *very* simple RNG so I can begin to understand how they work. The rules for the algorithm should be:

  -No memory reading and overwriting, it makes the algorithm too complex and I really don't need the security it provides at this point.
  -Using mostly logical operations for arithmetics (no mathematical terms to confuse me). If this is impossible, then try to keep it as simple as possible.
  -I believe it must be hard to retain randomness in a simple example but if it can be done without making the algorithm too complex then it would be appreciated.

Proper commenting of code will automatically earn the accepted answer 100 points extra. If you post a good comment or snippet regarding this question it might earn you a couple of points too. If this question is too hard for 100 points just say the word and I will raise it to 200.

And no, this is not homework. It's sheer entertainment and a personal challenge.

Marko
0
Comment
Question by:_marko_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6330963
static unsigned long int next = 1;

int rand(void){  /* RAND_MAX assumed to be 32767 */
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768;
}

void srand(insigned int seed){
    next = seed;
}
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6333544
Won't work an an SGI machine though, but it's a good sample :-)

regards,
CJ
0
 
LVL 2

Author Comment

by:_marko_
ID: 6335633
Thank you!

I tried it but the numbers seem to come out quite linear. I'll play around with it a bit tomorrow and see. The example seems great though! Not too complex :-)

Why won't it work on a SGI? It won't be ported to a machine dependent system, but it will be used in VHDL (by my girlfriend) at a later point.

Marko

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:CJ_S
ID: 6340237
On an SGI machine the integer number 32767 will give you a run-time error, it cannot handle the output generated. I don't exactly know why, due to our SGI machine not functioning properly anymore (we broke the monitor by accident).
0
 
LVL 2

Author Comment

by:_marko_
ID: 6342341
I'm sorry, I haven't yet had time to see through the code. I'll try to find some time tomorrow. Please be patient, I'm not one to leave a question hanging...

Marko
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6344436
Do a search on "Diehard" on the net. It has a tool to test the "randomness" of a generator, but the paper with it is very good.  Old as this is, I have never had a generator that passed all its tests to perform poorly.  My homemade ones that pass the tests do tend to be slower than simple methods such as rand() uses...  Note that endian issues make dos version give the wrong bits/bytes in the results, but mentally reverse this and its fine...

I would not use the one given above for a serious task, but for non-demanding programs it is fast and will be "good enough".  This is similar to the method used by many compilers in the built in generator.




0
 
LVL 2

Author Comment

by:_marko_
ID: 6353151
Yup, I played around with it a little, and I'm beginning to understand how RNG:s work, but I still have a long way to go before I can comprehend the more complex ones :)

CJ and jonnin, you can both pick up a few points for your time. Here are the locations:

CJ: http://www.experts-exchange.com/jsp/qShow.jsp?ta=progsoftgen&qid=20163519

jonnin: http://www.experts-exchange.com/jsp/qShow.jsp?ta=progsoftgen&qid=20163520

Regards,
Marko
0
 
LVL 84

Expert Comment

by:ozo
ID: 6353528
The simple RNG I gave, though widely used, is not a very good one.
here is a link to a better one.
http://www.math.keio.ac.jp/~matumoto/emt.html
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 99
VB.net and sql server 4 54
printf performancy 11 84
Eclipse Java import and method not resolved 4 76
In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question