Rand() with base

P

Profetas

Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks
 
J

Joona I Palaste

Profetas said:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks

"Base" and "length" do not apply to numbers, only representations of
them. Therefore you generate an 8 bit number the same way you generate
any other number.
Now an 8 bit number has values from 0 to 255. Therefore you only need to
"cut down" the number returned by rand() in either of two ways: you can
modulo it by 256 (%256) or bitwise-AND it with 255 (&255).

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 
P

Profetas

I need something like 1101011010101010.
to be generated randomly and automatically.
How do I specify the rand() limit? for example I want a random number of
10?
this way I can say I want a random number of 01.
 
J

Joona I Palaste

Profetas said:
I need something like 1101011010101010.

That's not an 8 bit number. Anyway, you need to understand that the
difference between bases only comes into play when you are printing
numbers out, or reading them in. Internally, a number is a number is a
number.
to be generated randomly and automatically.
How do I specify the rand() limit? for example I want a random number of
10?

I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
this way I can say I want a random number of 01.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
P

Profetas

-------------------------------------------------------
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
-------------------------------------------------------

That is what I ment sorry.
The base that I was saying is like base of 10 = decimal base o 8 octal
base of 2 binary. I wanted binary.
I think I will loop 16 times the rand()&1
I think I may get 0 and 1s.
Thanks
 
M

Mabden

Profetas said:
-------------------------------------------------------
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
-------------------------------------------------------

That is what I ment sorry.
The base that I was saying is like base of 10 = decimal base o 8 octal
base of 2 binary. I wanted binary.
I think I will loop 16 times the rand()&1
I think I may get 0 and 1s.
Thanks

int i, r, s;
randomize();
r = rand();
for (i=0; i < 16; i++) /* this could be a while loop */
{
s = (r >> 1) & 1;
}

This is untested but I think it's what you're trying to do.
 
J

Joona I Palaste

Profetas said:
-------------------------------------------------------
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
-------------------------------------------------------
That is what I ment sorry.
The base that I was saying is like base of 10 = decimal base o 8 octal
base of 2 binary. I wanted binary.
I think I will loop 16 times the rand()&1
I think I may get 0 and 1s.
Thanks

Now look here, the same numbers work in any base. Decimal, octal,
binary, or whatever. If you want 8-bit random numbers, just use
rand()%256 or rand()&255. They're all stored in the same way in the
computer memory no matter what base you think of them as.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
F

Frank Tombe

I would simply define a char: char b = rand(); i think this should be
enough.

Currently i have the problem that i want to have a true 32 bit random
number with rand(). How can i do that, RAND_MAX is not changeable....
 
A

Arthur J. O'Dwyer

I would simply define a char: char b = rand(); i think this should be
enough.

You mean
unsigned char = rand();

If 'char' is unsigned, your code is equivalent to the above.
If 'char' is signed, your code exhibits undefined behavior in the
vast majority of cases.
Currently i have the problem that i want to have a true 32 bit random
number with rand(). How can i do that, RAND_MAX is not changeable....

I'm almost certain this is a FAQ. The short answer is, learn
some arithmetic and use it. The long answer is, read the FAQ.

-Arthur
 
A

Alok Singhal

Now an 8 bit number has values from 0 to 255. Therefore you only need to
"cut down" the number returned by rand() in either of two ways: you can
modulo it by 256 (%256) or bitwise-AND it with 255 (&255).

As an aside, this probably is not the best way to generate 8-bit random
numbers: see http://www.eskimo.com/~scs/C-faq/q13.16.html

(int)((double)rand() / ((double)RAND_MAX + 1) * 256) is what the FAQ
recommends.

Alok
 
J

Joona I Palaste

As an aside, this probably is not the best way to generate 8-bit random
numbers: see http://www.eskimo.com/~scs/C-faq/q13.16.html
(int)((double)rand() / ((double)RAND_MAX + 1) * 256) is what the FAQ
recommends.

Note that the C standard does not specify any specific PRNG algorithm.
The FAQ is right for most implementations, but there could well be some
implementation where rand()%256 (or rand()&255, seeing as they're
equivalent) would work just as well.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
K

Keith Thompson

I would simply define a char: char b = rand(); i think this should be
enough.

This might work, or it might fail badly. Plain char may be either
signed or unsigned. Its size is 8 bits in most implementations, but
the standard only guarantees that it's at least 8 bits. The value of
RAND_MAX is at least 32767. Assigning a value larger than CHAR_MAX,
or smaller than CHAR_MIN, to a char object produces an overflow, which
(if char is signed) invokes undefined behavior; it typically grabs the
low-order bits, but this isn't guaranteed. Finally, the low-order
bits of the results returned by rand() are unreliable in many
implementations.

The OP wants to generate random numbers is a specified range (0..255).
Currently i have the problem that i want to have a true 32 bit random
number with rand(). How can i do that, RAND_MAX is not changeable....

RAND_MAX is guaranteed to be at least 32767, so rand() gives you at
least 15-bit random nubers. Call rand() 3 times and use bitwise
arithmetic to fill in your 32 bits. For example:

uint32_t result;

result = rand() & 0x7fff; /* 15 bits */
result <<= 15;
result |= rand() & 0x7fff; /* 15 bits */
result <<= 2;
result |= rand() & 0x0003; /* 2 bits */

If RAND_MAX happens to be at least 65535, you can call rand() only
twice.

If you don't trust the low-order bits of the results returned by
rand(), you might generate, say, 8 bits at a time using the method
described in question 13.16 of the C FAQ.

In many systems, rand() isn't very good, but there may be other random
number generators that give better results. If you don't mind writing
non-portable code, searching your documentation for "drand48" might be
fruitful; on some systems, /dev/random is a good source of random
bits. Since this is system-specific, the details are off-topic here.
 
M

Michael Wojcik

Currently i have the problem that i want to have a true 32 bit random
number with rand(). How can i do that, RAND_MAX is not changeable....

RAND_MAX has nothing to do with it. You can't get true random values
of any size from rand(); it's specified as a pseudorandom number
generator (see C90 7.10.2.1). If it were a true random number
generator in some (hosted) implementation (which would require an
external entropy source), srand() would not work as specified, and
the implementation would be non-conforming for at least two
functions.

If you're asking about generating 32-bit pseudorandom numbers, try
a Google Groups search, since this has been discussed any number of
times in c.l.c. Searching for "Marsaglia" will provide code for
CMWC generators, for example.

There's nothing in the standard library that's guaranteed to provide
a 32-bit PRNG.

Note that, depending on your intended use, seeding the generator may
be a far more difficult problem than implementing it in the first
place.

--
Michael Wojcik (e-mail address removed)

Reversible CA's are -automorphisms- on shift spaces. It is a notorious
fact in symbolic dynamics that describing such things on a shift of finite
type are -fiendishly- difficult. -- Chris Hillman
 
J

Joona I Palaste

RAND_MAX has nothing to do with it. You can't get true random values
of any size from rand(); it's specified as a pseudorandom number
generator (see C90 7.10.2.1). If it were a true random number
generator in some (hosted) implementation (which would require an
external entropy source), srand() would not work as specified, and
the implementation would be non-conforming for at least two
functions.

Does the standard specify that the same seed has to always generate the
same sequence of PRNs? But it doesn't specify which seed has to generate
which sequence?
 
A

Alok Singhal

Does the standard specify that the same seed has to always generate the
same sequence of PRNs? But it doesn't specify which seed has to generate
which sequence?

Yes, same seed generates the same sequence of PRNs.

This is from C99 7.20.2.2 (The srand function):

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.

Of course the standard doesn't specify the sequences for each seed. If
it did, then then u[1] won't have good or bad implementations of PRNGs,
just one implementation.

Alok

[1] just kidding, hope you don't mind :)
 
M

Mabden

Joona I Palaste said:
Michael Wojcik said:
Does the standard specify that the same seed has to always generate the
same sequence of PRNs? But it doesn't specify which seed has to generate
which sequence?

I am interested in this as well. I have a game that fills a bag with tiles
(or it could be a deck of cards, whatever) and I would like to just store a
(32-bit?) number that would re-generate the exact same sequence. It would
limit game to have 4billion possible beginnings, but that might just be
enough. ;-)
 
A

Arthur J. O'Dwyer

Well, obviously, the first seed has to generate the first sequence,
and... :) Yes, 'srand' is supposed to actually seed the PRNG, and 'rand'
is supposed to produce repeatable results. No, the Standard does not
specify which sequences 'rand' must produce --- that would be tantamount
to specifying the 'rand' algorithm, which the Standard doesn't do (and we
all know this because we have all read the FAQ, right? ;)
I am interested in this as well. I have a game that fills a bag with tiles
(or it could be a deck of cards, whatever) and I would like to just store a
(32-bit?) number that would re-generate the exact same sequence. It would
limit game to have 4billion possible beginnings, but that might just be
enough. ;-)

The Standard only guarantees 15 bits for RAND_MAX. You want a 32-bit
seed (in conforming C code), you get your own PRNG. Other than that,
yes, 'srand' will help you.

-Arthur
 
K

Keith Thompson

Joona I Palaste said:
Does the standard specify that the same seed has to always generate the
same sequence of PRNs? But it doesn't specify which seed has to generate
which sequence?

Yes. And if srand() isn't called, the effect is the same as calling
srand(1).
 
B

Ben Pfaff

Mabden said:
I am interested in this as well. I have a game that fills a bag with tiles
(or it could be a deck of cards, whatever) and I would like to just store a
(32-bit?) number that would re-generate the exact same sequence. It would
limit game to have 4billion possible beginnings, but that might just be
enough. ;-)

You could just use your own random number generator instead of
depending on the standard one. Then you could generate the same
sequences portably, instead of just within a particular version
of a particular implementation.
 
M

Mabden

Ben Pfaff said:
You could just use your own random number generator instead of
depending on the standard one. Then you could generate the same
sequences portably, instead of just within a particular version
of a particular implementation.

The way I do it now is to save the whole tilebag. It would be nice to have
an integer that I could use to re-create the same game. I was working in
some kind of hash function. I thought the rand() function used the system
time or something to change each number, each time you called it. I'm a
little surprised it can be so predictable.

A 16bit number would be OK, but maybe I should string a couple of rand()
generated numbers together.

Has anyone played with bitshifting and XOR'ing several rands to make them
more random, or perhaps XOR with the current time/date?

Something like put a rand() (15bit) into a 64bit number, shift a random
amount (<64bit), XOR or even "&" with another rand, loop again "X" times...

Just wondering if this has been tried, and whether this makes the number
more or less random....
 

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
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top