Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, March 30, 2016

Getting big random numbers in C/C++

Getting big random numbers in C/C++


Standard rand() function gives numbers not big enough for me: I need unsigned long long ones. How do we get really big random numbers? I tried modifying a simple hash function but it's too big, takes too long to run and never produces numbers which are less than 1e5!!

Answer by Baum mit Augen for Getting big random numbers in C/C++


You can easily do this with std::uniform_int_distribution.

Simple example code (taken from here, modified to use unsigned long long):

#include   #include     int main()  {      std::random_device rd;      std::mt19937 gen(rd());      std::uniform_int_distribution dis(lowerBorder, upperBorder);        for (int n=0; n<10; ++n)          std::cout << dis(gen) << ' ';      std::cout << '\n';  }  

Note that the seeding of the mersenne twister as done here for demo purposes is not perfect, for example see here.

Answer by nwellnhof for Getting big random numbers in C/C++


Here's a portable C99 solution that returns a random 64-bit number:

unsigned long long llrand() {      unsigned long long r = 0;        for (int i = 0; i < 5; ++i) {          r = (r << 15) | (rand() & 0x7FFF);      }        return r & 0xFFFFFFFFFFFFFFFFULL;  }  

Explanation: rand() returns integers in the range 0 to RAND_MAX and RAND_MAX is only guaranteed to be at least 32,767 (15 random bits). long long is guaranteed to have 64 bits but may be larger.

Answer by boramalper for Getting big random numbers in C/C++


You didn't ask for a specific OS and the answers here are really good, but on Linux (and probably on other OSes too) you can also read from a random device.

Example:

#include   #include     #define RANDDEV "/dev/urandom"    unsigned long long bigrand(void) {      FILE *rdp;      unsigned long long num;        rdp = fopen(RANDDEV, "rb");      assert(rdp);        assert(fread(&num, sizeof(num), 1, rdp) == 1);        fclose(rdp);        return num;  }  

Written on mobile, may have bugs. :P

Answer by VolAnd for Getting big random numbers in C/C++


If you want just to produce unsigned long long from value returned by rand() and do not care about the characteristics of the result consider the following function that must be compiler version and platform independent (because no "magic numbers" are used):

// this header has RAND_MAX value  #include     // and this header has ULLONG_MAX  #include     unsigned long long ullrand()  // Produces pseudo-random numbers from 0 to ULLONG_MAX  // by filling all bits of unsigned long long integer number  // with bits of several "small" integer numbers generated by rand()  {      unsigned long long myrndnum = 0; // at the beginning just zero      unsigned long long counter = ULLONG_MAX; // at the beginning we have all bits set as 1      // ... and while at least one bit is still set to 1      while(counter > 0) {             myrndnum = (myrndnum * (RAND_MAX + 1)) + rand(); // fill some bits from rand()             counter /= (RAND_MAX + 1); // decrease number of 1-bits in counter          }      // Return the result      return myrndnum;  }  

But if you want some sequence of random numbers with certain predetermined characteristics, you should look in some specific guides or math books. E.g. https://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html

Answer by AquariusCorsair for Getting big random numbers in C/C++


try this:

long N=1000000;  long randNumber;  for(long i=0;i


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.