Jump to content

  • Log In with Google      Sign In   
  • Create Account

Question about the build in Rand() function.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 JackBid   Members   -  Reputation: 453


Posted 27 April 2013 - 04:27 PM

So I think in just about every programming language there is some sort of build in Rand() function which chooses a random number between 0-1 which can then be multiplied to achieve a random point between two numbers.


My question is; in simple terms, does anyone know how the rand() function works? I was just thinking about it and don't really understand how it is possible to do. Theoretically what is going on inside that function?


P.S. I am a beginner and if anything said above was incorrect please forgive me. 


#2 Servant of the Lord   Crossbones+   -  Reputation: 17280


Posted 27 April 2013 - 05:17 PM

These are called "Pseudorandom number generators". 'pseudo' because they are not actually random, and do repeat, but their patterns are so far apart that it seems random.
If I want a number between 1 and 100, I can start off with '1', and add '6' to each value, my pattern of numbers goes like this:
1 7 13 19 ...snip... 85 91 97
Then the next value, 103, loops back around to '3'. Now it goes:
3 9 15 21 ...snip... 87 93 99
5 11 17 23 ...etc... 77 83 89 95
It generates 50 numbers before the pattern repeats. By 'pattern', it might touch the same value multiple times (it might generate '11' four or five times in the 50 numbers), but the full pattern of the generator doesn't repeat until after 50 numbers are generated.
In my example, the full pattern is:
1     7     13    19    25    31    37    43    49    55
61    67    73    79    85    91    97    3     9     15
21    27    33    39    45    51    57    63    69    75
81    87    93    99    5     11    17    23    29    35
41    47    53    59    65    71    77    83    89    95

Total: 50
If you have a 32 bit number (about 4 billion values) and create a algorithm that visits every number (hopefully an almost equal amount of times, not biased towards specific numbers), and does so in a way where the numbers can jump forward or backward (It might go 1 19 13 7 instead of 1 7 13 19 like my example did), then you'd have a decent pseudorandom generator.
You just need a more complex algorithm than just 'add 6'. Usually, they do this by manipulating the bits of the number, as well normal arithmetic.
Here's an example that provides better results (still not fantastic though), but is simple enough to understand.
There are many of them, some okay, others much better.
Having the results as a float between 0.0 and 1.0 instead of an integer between 0 and <max value> is the same idea, just different final representation.

Edited by Servant of the Lord, 27 April 2013 - 05:18 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

#3 superman3275   Crossbones+   -  Reputation: 2016


Posted 27 April 2013 - 05:59 PM

Servant Of The Lord, you just explained why how computers generate random numbers (it had been a mystery to me :))!


Cheers :)!

I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!


Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:


or Personal-Message me on here smile.png!

#4 Bacterius   Crossbones+   -  Reputation: 8188


Posted 27 April 2013 - 09:36 PM

The most common and simplest to implement is probably the LCG (linear congruential generator) which is pretty much just:


next value = (a * old value + b) mod m


For some initial value. With careful selection of the constants a, b, m, the generator will have maximum period equal to m values before repeating (in general, m is 2^32 or 2^64). But it kind of sucks in practice. If you plot the results they don't look random at all but display a lattice structure, even though individual values look random enough.


It is possible to implement true random number generation in computers, but it cannot be done completely in software due to determinism. For instance, under Linux, reading /dev/random will give you truly random bits, which are usually obtained from various unpredictable sources such as keystrokes, mouse movements, disk access latency, thermal noise, and can even use stuff produced by dedicated hardware (for instance, there are relatively cheap hardware random number generators based on quantum physics).


But for almost all applications a pseudorandom number generator will do fine. If you use C++11 you can check out the extra random number generators implemented in the standard library, that way you don't have to rely on rand() and can use any available generator without even having to code it yourself. Otherwise implementations exist in other languages, I am sure.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.


- Pessimal Algorithms and Simplexity Analysis

#5 jbadams   Senior Staff   -  Reputation: 17320


Posted 27 April 2013 - 10:04 PM

Note that when using pseudo-random numbers you're almost always able to provide a "seed" in some way; this is usually something you'll want to do once at the start of your program.


By providing a different seed you get a different sequence of random numbers, whilst providing the same one will give you the same sequence each time.  This can be useful if you're using your "random" numbers to generate maps, spawn bad guys, etc., because you can re-produce the same results each time by using the same seed values.

#6 iMalc   Crossbones+   -  Reputation: 2260


Posted 28 April 2013 - 03:26 AM

Most of them are really simple. Here's one from a common compiler:
long holdrand;

void __cdecl srand (unsigned int seed)
    holdrand = (long)seed;

int __cdecl rand (void)
    return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
As you can see, it's really really simple.
No rand() in C doesn't return a float from 0 to 1. It returns an int.

Here is an example of a much better PRNG: http://en.wikipedia.org/wiki/Xorshift
Still quite simple, but far far better.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.