Random number in c++

This is really more of a reminder for myself… how to draw random numbers fast in c/c++. (download [random.cpp]) and compile like so: g++ -O2 -o random random.cpp

#include ‹iostream›
#include ‹cmath›
#include ‹fstream›
#include ‹cstdlib›
using namespace std;
#define PI M_PI
#define RAD PI/180.0
#define SIG5 5.0*sig
#define SIG10 10.0*sig
#define SIG20 20.0*sig
unsigned long long int rdtsc();
int main (int argc, char* argv[]) //take in commandline arguements
{
int ii = 0;
srandom(rdtsc());
while (ii ‹ 100)
{
cout ‹‹ rand(); //RAND_MAX;
cout ‹‹ “t”;
cout ‹‹RAND_MAX;
cout ‹‹ “n”;
ii +=1;
}
return 0;
}
unsigned long long int rdtsc(void) //Call a Random number.
{
unsigned long long int x;
unsigned a, d;
__asm__ volatile(“rdtsc” : “=a” (a), “=d” (d));
return ((unsigned long long)a) | (((unsigned long long)d) ‹‹ 32);;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: