C: A true random number generator: Difference between revisions

From FSDeveloper Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 15: Line 15:
   return (int)i;
   return (int)i;
  }
  }
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]]

Revision as of 12:49, 30 April 2018

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.

#include <intrin.h>
#pragma intrinsic(__rdtsc)

int getRand(int iMin,int iMax)
{
  unsigned __int64 i;

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

  return (int)i;
}