• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1010

# A simple RNG algorithm?

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
_marko_
• 3
• 2
• 2
• +1
1 Solution

Commented:
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

Commented:
Won't work an an SGI machine though, but it's a good sample :-)

regards,
CJ
0

Author Commented:
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

Commented:
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

Author Commented:
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

Commented:
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

Author Commented:
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

Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.