I am trying to assign to a variable a number between 0 and 65535 exactly, but i can't because RAND_MAX
is set to 32767. How can I generate a random number bigger than 32767 and at the same time with the possibility that the number generated is between 0 and 65535?
Asked
Active
Viewed 3,031 times
2

Keith Thompson
- 254,901
- 44
- 429
- 631

Lovenkrands
- 165
- 1
- 10
-
9Use the C++11 `
` functions. – Brian Bi Mar 07 '15 at 00:34 -
2Following up on @Brian, please read [this excellent article on the use of `
` tools and why `rand()` is generally bad](http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/). – zopieux Mar 07 '15 at 00:45
2 Answers
6
I would try something like this:
int msb = (rand() % 2) << 15;
int random = msb | rand();
That is, I generate two random numbers. The first one (msb) will set the Most Significant Bit of the second (random).

Milack27
- 1,619
- 2
- 20
- 31
-
if int is 32 bits then msb could become much larger than 65535 after shifting. may be you mean short msb = rand() << 15. – engf-010 Mar 07 '15 at 00:49
-
You're right. I would have to mask the 16 most significant bits to have the desired effect. – Milack27 Mar 07 '15 at 00:52
-
Thanks. Two questions. What is << 15 doing?, and in the second line what is the | operator doing there? – Lovenkrands Mar 07 '15 at 02:32
-
The standard rand() function generates a random number of 15 bits, going from 0 to 32767, as you've mentioned. To extend this range to 65535, we have to add one more bit. So, in the first line, I'm generating a random number between 0 and 1, that is, rand() % 2. The << 15 is shifting this result 15 bits to the left, turning msb into the most significant bit. The | operator is a bitwise OR, and its function in my code is to put the extra bit in the new random number generated. If I had used the + operator, the result would be the same (in this case, not always). – Milack27 Mar 07 '15 at 02:50
-
I understand. But, going deep, why is a | operator doing the function of the + operator; why it does work successfully? – Lovenkrands Mar 07 '15 at 02:59
-
Because in this case, msb is an unknown bit followed by 15 zeros. On the other hand, rand() is a zero bit followed by 15 unknown bits. Like this, in binary: msb = x000000000000000; rand() = 0xxxxxxxxxxxxxxx. So, msb + rand() = msb | rand() = xxxxxxxxxxxxxxxx. Consider x as an unknown bit. When doing an OR operation between a zero bit and x, the result is the same x. Because msb and rand() are "complementary", the bitwise OR results the same as a simple addition. – Milack27 Mar 07 '15 at 03:07
6
If your compiler supports C++11 you can do something like this:
#include <iostream>
#include <random>
int main()
{
std::random_device rd;
std::default_random_engine eng {rd()};
std::uniform_int_distribution<> dist(0, 65535);
for(unsigned i = 0; i < 10; ++i)
std::cout << dist(eng) << '\n';
}
Sample output:
38198
49494
41521
10688
1262
51014
36094
16740
1212
59184

Galik
- 47,303
- 4
- 80
- 117
-
Can you modify the syntaxis without the std:: ? (when "using namespace std;") – Lovenkrands Mar 07 '15 at 00:52
-
2@user3273960 I would never recommend doing that: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice but it would definitely work. – Galik Mar 07 '15 at 00:56
-
Yes, it's not because i want to but because i need to do it with namespace. Question: With namespace: uniform_int_distribution<> dist(0, 65535); stays like this? – Lovenkrands Mar 07 '15 at 02:45
-
-
-
@JenilDave Yes, you should be able to find some library code that does it. Otherwise @Milack27's answer will work in `C`. – Galik May 17 '20 at 07:34
-
@Galik, thanks for your help, but i figured out my way, i tried this : ```int start=50000; int stop =60000; srand(time(0)); int lsb = rand()%(stop-start); lsb = start+lsb;``` What do you think about this? – JenilDave May 17 '20 at 07:42