Solved

A simple RNG algorithm?

Posted on 2001-07-28
8
972 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_
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now