Solved

A simple RNG algorithm?

Posted on 2001-07-28
8
979 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordlen challenge 3 67
python sqlite question 11 46
Base1 Encode/Decode 3 77
Change the background and font colors in Notepad++ 5 87
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…

777 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