I do have a function in C to give me a unsigned short random number from 0 to 65535.
How can I make it to give me a greater number without much calculating? (r * 65536 + r)

I've made a function in C to do that for me:

static unsigned long convert_random_to_long()
static unsigned char row[4];
static unsigned short *p1=&row[0];
static unsigned short *p2=&row[2];
static unsigned long *long_random=&row[0];


return *long_random;

It gave me a few warnings but worked well.
In C++ it is not giving anymore warnings, simply ends up in errors because of the strict type checking.

How could I make my function work in C++?

Thank you in advance,

5 Years
Discussion Span
Last Post by misi

Unfortunately I cannot edit my post.
Please read line 9 as *p2=random();


In C++, you need to do the cast explicitly:

static unsigned char row[4];
static unsigned short *p1 = reinterpret_cast<unsigned short *>(&row[0]);
static unsigned short *p2 = reinterpret_cast<unsigned short *>(&row[2]);
static unsigned long *long_random = reinterpret_cast<unsigned long *>(&row[0]);

That should solve your problem.

Votes + Comments

You must explicitly inform your C++ compiler that you wish to convert the short pointer type to a long pointer type.

static unsigned long* long_random = (static unsigned long*) row;//make row an array of 2 shorts

I don't think you need a character array here. Just use two shorts. And are you sure you want to use static here?

Alternately, you could just do this:

unsigned long value;
value = rand();
value = (value<<16)|rand();

How this works is simple. After value = rand() might have something in binary that looks like this...note I didn't count the digits lol just assume it's 32 bits:
0000000000000000000001010101010101010101010 or some such, where the 2nd half is your random number.
now when you do value<<16, you left shift all that shit, so you now have

then, you combine that with another random number using the | character, which stands for OR, so you have, for example

1010101010101010101010000000000000000000000  //value<<16
0000000000000000000001111111101010110111111  //rand()
Votes + Comments
Thank you it's working!

Thank you very much guys, problem solved.
Mike was spot on!


cout << *long_random;

The result is: 65537 which is absolutely corrrrrect. (1*65536+1)

Great team here!!!

With the help of this I can generate even larger random numbers without the headaches of calculations.

Thinking about Greywolf's suggestion...

Edited by misi


Just use two shorts.

Good idea,thank you.

And are you sure you want to use static here?

Yes, I want to use static because this way the machine has to calculate the addresses only once while the program is running.


At Greywolf:

And are you sure you want to use static here?

Checking the code again I found that what you meant with that static.
No,I did not want that.Thank you.
The function is now : unsigned long convert_random_to_long()

unsigned long long convert_random_to_long_long()


cout << *long_long_random << "\n";

cout << *long_long_random << "\n";

The result of the test:


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.