srand with big numbers

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have read and studied and looked at references and can't find out how
to do this. Here's where some real knowledge comes in now lets see who in
clc really knows there stuff ;)

main(void) {
srand(time(NULL));
printf("%i\\n",rand());
return 0;

Simple and forward now here's where I'm stumped. I want the random values
returned in the above way to be long doubles and the equivalent of
pow(2,128). But srand takes an unsigned 32 bit on my system and long double
is 96 bit on my system, or 12 bytes. I can find no conversion functions and
I don't know if I need casts. Can srand return random values of 2 to the 128
power? I know this can be done. Someone please show me.

Thanks
 
D

Dann Corbit

Bill Cunningham said:
I have read and studied and looked at references and can't find out how
to do this. Here's where some real knowledge comes in now lets see who in
clc really knows there stuff ;)

main(void) {
srand(time(NULL));
printf("%i\\n",rand());
return 0;

Simple and forward now here's where I'm stumped. I want the random values
returned in the above way to be long doubles and the equivalent of
pow(2,128). But srand takes an unsigned 32 bit on my system and long
double is 96 bit on my system, or 12 bytes. I can find no conversion
functions and I don't know if I need casts. Can srand return random values
of 2 to the 128 power? I know this can be done. Someone please show me.

The C-FAQ has this:

13.16: How can I get random integers in a certain range?

A: The obvious way,

rand() % N /* POOR */

(which tries to return numbers from 0 to N-1) is poor, because
the low-order bits of many random number generators are
distressingly *non*-random. (See question 13.18.) A better
method is something like

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

If you'd rather not use floating point, another method is

rand() / (RAND_MAX / N + 1)

Both methods obviously require knowing RAND_MAX (which ANSI
#defines in <stdlib.h>), and assume that N is much less than
RAND_MAX.

(Note, by the way, that RAND_MAX is a *constant* telling you
what the fixed range of the C library rand() function is. You
cannot set RAND_MAX to some other value, and there is no way of
requesting that rand() return numbers in some other range.)

If you're starting with a random number generator which returns
floating-point values between 0 and 1, all you have to do to get
integers from 0 to N-1 is multiply the output of that generator
by N.

References: K&R2 Sec. 7.8.7 p. 168; PCS Sec. 11 p. 172.


You won't be able to use integers to compute the values.
You won't be able to get full precision on the answers if you want to be
able to represent all values between largest and smallest since
2^128=340282366920938463463374607431768211456
has 39 decimal digits and 96 bit floating point cannot possibly represent
that many in perfect accuracy.
 
I

Ian Collins

Bill said:
I have read and studied and looked at references and can't find out how
to do this. Here's where some real knowledge comes in now lets see who in
clc really knows there stuff ;)

main(void) {
srand(time(NULL));
printf("%i\\n",rand());
return 0;

Simple and forward now here's where I'm stumped. I want the random values
returned in the above way to be long doubles and the equivalent of
pow(2,128). But srand takes an unsigned 32 bit on my system and long double
is 96 bit on my system, or 12 bytes. I can find no conversion functions and
I don't know if I need casts. Can srand return random values of 2 to the 128
power? I know this can be done. Someone please show me.
srand() seeds a pseudo-random number generator, it does not return anything.

rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
to be 32767.

You will have to use a platform extension to get bigger random numbers.
 
S

Sjouke Burry

Bill said:
I have read and studied and looked at references and can't find out how
to do this. Here's where some real knowledge comes in now lets see who in
clc really knows there stuff ;)

main(void) {
srand(time(NULL));
printf("%i\\n",rand());
return 0;

Simple and forward now here's where I'm stumped. I want the random values
returned in the above way to be long doubles and the equivalent of
pow(2,128). But srand takes an unsigned 32 bit on my system and long double
is 96 bit on my system, or 12 bytes. I can find no conversion functions and
I don't know if I need casts. Can srand return random values of 2 to the 128
power? I know this can be done. Someone please show me.

Thanks
Google : long period random generator
 
V

vippstar

it's not guaranteed to be written to stdout (ie no flush performed)
because instead of \n you had \\n, missing prototype of printf
forgot } here
srand() ha nothing to do with the size of what rand() returns, so,
nonsense.Now baiting someone to explain him what a "conversion" is or what he
*might* mean with itFirst he asks then he claims he knows it can be done.
Baiting someone to post a long rant about *why* it cannot be done

conclusion: TROLL.
Everything I commented at is things regulars could argue for hours
with huge posts. A waste of time for everyone.
It's clear Bill Cunningham is a troll! Why cannot the community ignore
him like the other trolls?
srand() seeds a pseudo-random number generator, it does not return anything.

rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
to be 32767.
At *least* 32767, it could be 2^31-1 for example (which is the case on
my implementation).
Don't reply to trolls.
 
B

Bill Cunningham

Google : long period random generator

;) You got it. I am trying to learn by creating a UUID generator. I know I
can get one but how do you learn from that. Thanks

Bill
 
B

Bill Cunningham

Ian Collins said:
The drand48 family if you are using a Unix/Linux platform.

Try a platform specific programming group.
This is probably written in C so C can do this. It's just in the std
library. THanks Ian. I'm seeing erand and jrand too.

Bill
 
B

Bartc

srand() seeds a pseudo-random number generator, it does not return
anything.

rand() an int the range of 0 to RAND_MAX. RAND_MAX is only guaranteed
to be 32767.

You will have to use a platform extension to get bigger random numbers.

I just use something like this:

#define bigrand (rand()<<16 + rand())

Any particular problem with that? The randomness is not critical.
 
P

Philip Potter

Bartc said:
I just use something like this:

#define bigrand (rand()<<16 + rand())

Any particular problem with that? The randomness is not critical.

Suppose for a moment that the period of rand() is 65536. The period of
your aggregate generator bigrand will be 32768 - that is, 32768 samples
after the first random sample, you will get the same sequence out again.
Aggregating the outputs together cannot increase the period.

If the randomness is not critical, then perhaps that's good enough for
you. I can't answer that for you.

Philip
 
P

Philip Potter

Bartc said:
I just use something like this:

#define bigrand (rand()<<16 + rand())

Any particular problem with that? The randomness is not critical.

Suppose for a moment that the period of rand() is 65536. The period of
your aggregate generator bigrand will be 32768 - that is, 32768 samples
after the first random sample, you will get the same sequence out again.
Aggregating the outputs together cannot increase the period.

If the randomness is not critical, then perhaps that's good enough for
you. I can't answer that for you.

Philip
 
B

Ben Bacarisse

Bartc said:
I just use something like this:

#define bigrand (rand()<<16 + rand())

Any particular problem with that? The randomness is not critical.

Yes. You probably meant ((rand()<<16) + rand()). Frequent UB otherwise.

As for the crrected version... tt depends. If RAND_MAX is only 32767
you get two "dead bits" in the result. Worse, if the system has 16
bit ints the shift is undefined. (unsigned long)rand() << 16 is safer.
 
B

Bill Cunningham

Yes. You probably meant ((rand()<<16) + rand()). Frequent UB otherwise.

As for the crrected version... tt depends. If RAND_MAX is only 32767
you get two "dead bits" in the result. Worse, if the system has 16
bit ints the shift is undefined. (unsigned long)rand() << 16 is safer.

--
Is this an example of what's called a "macro expansion" in C ? I haven't
got there yet but I will try this.

Bill
 
C

CBFalconer

Bill said:
I have read and studied and looked at references and can't find
out how to do this. Here's where some real knowledge comes in now
lets see who in clc really knows there stuff ;)

main(void) {
srand(time(NULL));
printf("%i\\n",rand());
return 0;

Simple and forward now here's where I'm stumped. I want the random
values returned in the above way to be long doubles and the
equivalent of pow(2,128). But srand takes an unsigned 32 bit on my
system and long double is 96 bit on my system, or 12 bytes. I can
find no conversion functions and I don't know if I need casts. Can
srand return random values of 2 to the 128 power? I know this can
be done. Someone please show me.

7.20.2.1 The rand function

Synopsis
[#1]
#include <stdlib.h>
int rand(void);

.... snip ...

Returns

[#4] The rand function returns a pseudo-random integer.

Environmental limits

[#5] The value of the RAND_MAX macro shall be at least
32767.

7.20.2.2 The srand function

Synopsis
[#1]
#include <stdlib.h>
void srand(unsigned int seed);

Description

[#2] The srand function uses the argument as a seed for a
new sequence of pseudo-random numbers to be returned by
subsequent calls to rand. If srand is then called with the
same seed value, the sequence of pseudo-random numbers shall
be repeated. If rand is called before any calls to srand
have been made, the same sequence shall be generated as when
srand is first called with a seed value of 1.

[#3] The implementation shall behave as if no library
function calls the srand function.

Returns

[#4] The srand function returns no value.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top