Thread: RAND() Does it favor the lower number?

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005

    Lightbulb RAND() Does it favor the lower number?

    I thought so once. I had created a simulation where creatures live in my artifisial enviorment as is they, move around, eat, reproduce, and die. Now, I used RAND() to detirmain the random locations of the food, creatures and sex of the creatures. And I noticed that most of the creatures where female. In my code 1 ment a male, and 0 meant a female. Also most of the creatures and food seemed to allways popup near the x-y coords of 0x0. (Near the top left.) So I threw together a program that checked the average. Here is my code:

    #include <windows.h>
    #define MAX_TIMES 500000
    int WINAPI WinMain (HINSTANCE hThisInstance,
    					HINSTANCE hPrevInstance,
    					LPSTR lpszArgument,
    					int nFunsterStil)
    	char szText[MAX_PATH * 10];
    	strcpy(szText, "This program will run the RAND() function\nthrough a loop, then display the average.");
    	MessageBox(NULL, szText, "Random Generator Tester", MB_ICONINFORMATION);
    	int nRand[MAX_TIMES];
    	int nResualt;
    	for(int i = 0; i < MAX_TIMES; i++){
    	nRand[i] = rand()%1000;
    	nResualt += nRand[i];  }
    	nResualt /= MAX_TIMES;
    	wsprintf(szText, "After running RAND() five hundred thousand times at\nthe value of 1000, the average has been gauged at %d.", nResualt);
    	MessageBox(NULL, szText, "Random Generator Tester", MB_ICONINFORMATION);
    	return 0;
    Amazingly, the result is 496. So it does favor the lower number a tiny bit, but I expected more so than this.

  2. #2
    Registered User
    Join Date
    Apr 2002
    void srand(unsigned int seed);
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Using modulo to restrict the range of random numbers generated throws out the higher order bits. This is explained in the FAQ about two-thirds of the way down the page. There is also a suggestion as to how to avoid that problem. Unfortunately we are still dealing with random numbers (actually pseudo-random) and though in the long term one would expect an even distribution of values and therefore a statistical average approaching the mid point of the range, there would still be the chance for this average to not be near the mid-point (the greater the sample the closer to the middle the average should turn out). Issues of the distribution are also addressed in the FAQ article mentioned.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2006
    You could do some bit transposing before taking modular residues to mask this discrepancy; or use some other "more random" generator, like Mersenne Twister or ISAAC. (I'd recommend MT.)

    Or declare it a program "feature" and tell your users this is all part of the challenge
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    There is an extensive discussion about this here:

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 07:57 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM