G
Guest
How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
the RAND_MAX is the max of rand function. and equal to 0x7fff
海风 said:How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
海风 said:How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
?? said:How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
osmium said:Use a shift operator and add the result of two (or more) calls on rand()
into a larger type.
Sadly, this is not a sure thing. For instance, on Windows [VC++ 2003],S. I. Becker said:海风 said:How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
If RAND_MAX = 0x7fff... then you can use:
unsigned int r = ~rand();
If (as I believe is common) RAND_MAX is the largest [signed] int (e.g.
0x7fff in 16bit, 0x7fffffff in 32 bit), then you get a random number
from RAND_MAX + 1 to 2 * RAND_MAX - 1 (0x8000... to 0xffff...) inclusive.
Proof left as an exercise to the reader.
HTH,
Stewart
You can also make 1 random long integerMarkus said:This is a quality of implementation issue. The implementation I am
using has RAND_MAX equal to 0x7fffffff.
Anyway you can use the boost random library which was by the way
accepted into the draft C++0x standard.
How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
Steve Pope said:On what system is RAND_MAX that small? I'm seeing 2^31 - 1
on gcc/linux, which is 0x7fffffff.
Jack said:Sadly, this is not a sure thing. For instance, on Windows [VC++ 2003],S. I. Becker said:If (as I believe is common) RAND_MAX is the largest [signed] int (e.g.
0x7fff in 16bit, 0x7fffffff in 32 bit), then you get a random number
from RAND_MAX + 1 to 2 * RAND_MAX - 1 (0x8000... to 0xffff...) inclusive.
RAND_MAX is only 0x7fff, despite the fact that my integers are most
assuredly 32 bits.
海风 said:How to generate a random integer that is bigger than RAND_MAX.
the RAND_MAX is the max of rand function. and equal to 0x7fff
Sjouke Burry said:You can also make 1 random long integer
out of 2 short ones.
osmium said:"Steve Pope" writes:
I can't begin to imagine why the answer to that question would be useful to
anyone. But Turbo C++ for Windows was one such platform, and I suspect their
are many more.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.