« Symbols in matplotlib | Main | Flames vs Oilers »

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);;
}

TrackBack

TrackBack URL for this entry:
http://krioma.net/cgi-bin/MT-3.38/mt-tb.cgi/462

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on November 9, 2010 2:10 AM.

The previous post in this blog was Symbols in matplotlib.

The next post in this blog is Flames vs Oilers.

Many more can be found on the main index page or by looking through the archives.

Get Firefox! Valid XHTML 1.0! Valid CSS! RSS Feed BlogUniverse - listed Powered by Apache Creative Commons License ringsofsaturnrock's Most Interesting Photos on Flickriver

Powered by
Movable Type 3.38