Solved

# A simple RNG algorithm?

Posted on 2001-07-28
Medium Priority
992 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
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

ozo earned 400 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

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

regards,
CJ
0

LVL 2

Author Comment

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

LVL 22

Expert Comment

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

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

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

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

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

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project
###### Suggested Courses
Course of the Month8 days, 15 hours left to enroll

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

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