generate random long number in C++

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

We know that to generate random long number we do following steps in Java

Random r=new Random()  return r.nextLong(); 

what will be equivalent of this code in C++? like this?

return (long) rand(); 

回答1:

<cstdlib> provides int rand(). You might want to check out the man page. If long is bigger than int on your system, you can call rand() twice and put the first value in the high word.

#include <cstdlib>  long lrand() {     if (sizeof(int) < sizeof(long))         return (static_cast<long>(rand()) << (sizeof(int) * 8)) |                rand();      return rand(); } 

(it's very unlikely that long is neither the same as or double the size of int, so this is practical if not theoretically perfect)

Check your docs for rand() though. It's not a great generator, but good enough for most things. You'll want to call srand() to initialise the random-number generation system. Others have commented that Windows doesn't return sizeof(int) randomised bits, so you may need to tweak the above.



回答2:

Using boost random library can save you of quite nasty surprises with (pseudo)random numbers



回答3:

First, you have ton know that in the current standard C++ there is no random library. In fact there is one, but it's available in a sperate namespace called TR1 because it's the result of a Technical Report done in 2003. It will be available in the standard library for the next standard (coming next year if all goes well).

So if you have a recent compiler (VS2008 or lasts versions of GCC) you have access to the std::tr1::random library; If you have a compiler implementing the parts of the next standard, then you have it std::random.

If you don't have access to that library, there is an implementation available in the boost libraries : http://www.boost.org/doc/libs/1_44_0/doc/html/boost_random.html

Now in all cases, the way to get a random number is the same as it's all the same library (from the boost doc):

boost::mt19937 rng;                 // produces randomness out of thin air                                     // see pseudo-random number generators boost::uniform_int<> six(1,6);      // distribution that maps to 1..6                                     // see random number distributions boost::variate_generator<boost::mt19937&, boost::uniform_int<> >          die(rng, six);             // glues randomness with mapping int x = die();                      // simulate rolling a die 


回答4:

Portable hack:

 long r = 0; for (int i = 0; i < sizeof(long)/sizeof(int); i++) {     r = r << (sizeof(int) * CHAR_BITS);     r |= rand(); } return r; 

Why do you need a random long anyway?



回答5:

This is the method I use. It is returning numbers in range [0, 2^64-1].

unsigned long long unsignedLongLongRand() {     unsigned long long rand1 = abs(rand());     unsigned long long rand2 = abs(rand());     rand1 = rand1 << (sizeof(int)*8);        unsigned long long randULL = (rand1 | rand2);        return randULL; } 


回答6:

this function works like rand() and uses Unsigned Long Type:

unsigned long _LongRand () {  unsigned char MyBytes[4]; unsigned long MyNumber = 0; unsigned char * ptr = (unsigned char *) &MyNumber;  MyBytes[0] = rand() % 256; //0-255 MyBytes[1] = rand() % 256; //256 - 65535 MyBytes[2] = rand() % 256; //65535 - MyBytes[3] = rand() % 256; //16777216  memcpy (ptr+0, &MyBytes[0], 1); memcpy (ptr+1, &MyBytes[1], 1); memcpy (ptr+2, &MyBytes[2], 1); memcpy (ptr+3, &MyBytes[3], 1);  return(MyNumber); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!