C: A true random number generator

From FSDeveloper Wiki
Jump to navigationJump to search

This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.

[code]

  1. include <intrin.h>
  2. pragma intrinsic(__rdtsc)


// ------------------------------ //Randomiser - seed by CPU cycles // ------------------------------

int getRand(int iMin,int iMax) {

 unsigned __int64 i;  
 i = __rdtsc();
 do
   i=(int)rand()%iMax;
 while (i < iMin);
 return (int)i;

} [/code]