Member Avatar for misi

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];

*p1=random();
*pt=random();

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,
misi

Recommended Answers

All 7 Replies

Member Avatar for 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.

commented: Bullseye! +0

Unfortunately, we can't see your screen to see what the errors and warnings are.

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
1010101010101010101010000000000000000000000

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()
=
1010101010101010101011111111101010110111111
commented: Thank you it's working! +0
Member Avatar for misi

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

*p1=1;
*p2=1;

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...

Member Avatar for 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.

Member Avatar for misi

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()

and:
unsigned long long convert_random_to_long_long()

*p1=1;
*p2=1;
*p3=1;
*p4=65535;

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

*p1=random();
*p2=random();
*p3=random();
*p4=random();
cout << *long_long_random << "\n";

The result of the test:

18446462603027873793
10356125374112773240

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.