By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

Rand() with base

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

Nov 14 '05 #1
Share this Question
Share on Google+
36 Replies


P: n/a
Profetas <xu*****@yahoo.com> scribbled the following:
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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #2

P: n/a
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.
Nov 14 '05 #3

P: n/a
Profetas <xu*****@yahoo.com> scribbled the following:
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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #4

P: n/a
-------------------------------------------------------
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

Nov 14 '05 #5

P: n/a
"Profetas" <xu*****@yahoo.com> wrote in message
news:77******************************@localhost.ta lkaboutprogramming.com...
-------------------------------------------------------
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.
Nov 14 '05 #6

P: n/a
Profetas <xu*****@yahoo.com> scribbled the following:
-------------------------------------------------------
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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 14 '05 #7

P: n/a
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....
Nov 14 '05 #8

P: n/a
On 2004-05-20, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
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
Nov 14 '05 #9

P: n/a
Alok Singhal <al**********@hotmail.com> scribbled the following:
On 2004-05-20, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
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.


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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #10

P: n/a

On Thu, 20 May 2004, Frank Tombe wrote:

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
Nov 14 '05 #11

P: n/a
nc******@freequote.net (Frank Tombe) writes:
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).
The FAQ says how to do this: see <http://www.eskimo.com/~scs/C-faq/top.html>,
question 13.16.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12

P: n/a

In article <9d**************************@posting.google.com >, nc******@freequote.net (Frank Tombe) writes:

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 mi************@microfocus.com

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
Nov 14 '05 #13

P: n/a
Michael Wojcik <mw*****@newsguy.com> scribbled the following:
In article <9d**************************@posting.google.com >, nc******@freequote.net (Frank Tombe) writes:
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.


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?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 14 '05 #14

P: n/a
On 2004-05-21, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
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 :)
Nov 14 '05 #15

P: n/a
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c8**********@oravannahka.helsinki.fi...
Michael Wojcik <mw*****@newsguy.com> scribbled the following:
In article <9d**************************@posting.google.com >, nc******@freequote.net (Frank Tombe) writes:
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.


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. ;-)
Nov 14 '05 #16

P: n/a

On Fri, 21 May 2004, Mabden wrote:

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote...

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?

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

Nov 14 '05 #17

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Michael Wojcik <mw*****@newsguy.com> scribbled the following:
In article <9d**************************@posting.google.com >,
nc******@freequote.net (Frank Tombe) writes:
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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #18

P: n/a
"Mabden" <ma****@sbcglobal.net> writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #19

P: n/a
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@blp.benpfaff.org...
"Mabden" <ma****@sbcglobal.net> writes:
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.


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

--
Mabden
Nov 14 '05 #20

P: n/a
On Fri, 21 May 2004 22:32:07 GMT, "Mabden" <ma****@sbcglobal.net>
wrote in comp.lang.c:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@blp.benpfaff.org...
"Mabden" <ma****@sbcglobal.net> writes:
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.


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.


I think you misunderstand the primary purpose for PRNGs, the one "more
important" than games.

PRNGs are often used to generate data to test algorithms and programs,
particularly statistical, engineering, and scientific analysis
programs. It can be necessary to compare such programs by having them
process exactly the data sequence. If it were not possible to
recreate a specific sequence from a PRNG whenever needed, people doing
this sort of work would need to generate large files of data sequences
just so they could feed them into different programs.

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


<sigh>

There is an enormous amount of information about PRNGs available on
the web, try a Google search. But in general, trying to make the
results from a PRNG "more random" are much more likely to make the
less so.

If you want 32-bit pseudo random numbers, you can find source code for
many of them on the web. That also frees you from dependence on any
particular compiler's implementation of rand().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #21

P: n/a

On Fri, 21 May 2004, Mabden wrote:

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.
'rand' has to be predictable; otherwise, how could you ever test it
for correctness? Besides, the system timer is a notoriously bad way of
getting true random bits; if the C standard library seemed to bless it
in any way, that would just encourage bad security products. And Unix
(C's natural habitat :) takes its security *very* seriously.

If you want true randomness, there exist RNG servers for such
purposes, online. But then you can't use portable C to get online,
so it's OT either way.
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?
srand((unsigned)time(0)) is the "canonical" way to get a random seed
into the PRNG. Of course it's not secure, but it's good enough for
games and things that you won't ever need to run twice in close succession
expecting different outputs.
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...


You are describing the "hashing" of a random number. This is the
second law of thermodynamics speaking: No matter how much you screw
around with a random variable, it's not gonna get any more random.
It can get *less* random, of course (shifting bits off the end will
lose those bits forever, e.g.), but you can't make randomness (a.k.a.
"information"; perversely, these are basically the same thing at the
information-theory level) from nothing.
Take further discussion of random numbers, security, and information
theory to one of comp.programming or sci.crypt, please.

-Arthur
Nov 14 '05 #22

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
[...]
'rand' has to be predictable; otherwise, how could you ever test it
for correctness?
Well, you could perform statistical tests. In fact, a good test of
rand() should both verify that it generates a consistent sequence for
a given seed, and that each sequence meets certain statistical
criteria (though failing the latter doesn't necessarily imply
non-compliance).
srand((unsigned)time(0)) is the "canonical" way to get a random seed
into the PRNG. Of course it's not secure, but it's good enough for
games and things that you won't ever need to run twice in close succession
expecting different outputs.


That's assuming that the value returned by time(0), cast to unsigned,
varies significantly over a reasonable period of time. If time()
returns a double representing the number of days since some epoch, the
value of (unsigned)time(0) will only change once per day. I know of
no such implementation, but you might as well avoid non-portable
assumptions if you can.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #23

P: n/a

On Sat, 22 May 2004, Keith Thompson wrote:

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
[...]
'rand' has to be predictable; otherwise, how could you ever test it
for correctness?
Well, you could perform statistical tests. In fact, a good test of
rand() should both verify that it generates a consistent sequence for
a given seed, and that each sequence meets certain statistical
criteria (though failing the latter doesn't necessarily imply
non-compliance).


But if it weren't repeatable, your results wouldn't be reproducible...
:) Okay, it's possible. I just think it would be harder to prove
the correctness of a real RNG (under all possible conditions) than of
a PRNG (under ditto). For one thing, you'd have to do that statistical
proof (*not* test; tests don't prove anything, least of all correctness ;)
on whatever the *source* of true randomness was, and that would be
*really* tricky, I think.
srand((unsigned)time(0)) is the "canonical" way to get a random seed
into the PRNG. Of course it's not secure, but it's good enough for
games and things that you won't ever need to run twice in close succession

expecting different outputs.[*]
That's assuming that the value returned by time(0), cast to unsigned,
varies significantly over a reasonable period of time. If time()
returns a double representing the number of days since some epoch, the
value of (unsigned)time(0) will only change once per day.

[*] ...For suitable values of "in close succession."

-Arthur
Nov 14 '05 #24

P: n/a
Arthur J. O'Dwyer wrote:

[SNIP]


But if it weren't repeatable, your results wouldn't be reproducible...
:) Okay, it's possible. I just think it would be harder to prove
the correctness of a real RNG (under all possible conditions) than of
a PRNG (under ditto). For one thing, you'd have to do that statistical
proof (*not* test; tests don't prove anything, least of all correctness ;)
on whatever the *source* of true randomness was, and that would be
*really* tricky, I think.
Many decades ago, I remember that to check for randomness we would
use the Chi-Square test. (Google for it.) But the instructor
insisted that we run enough tests that we could also run a
Chi-Square of the Chi-Square results. The first set of
results should also follow some kind of random pattern
as determined by the second Chi-Square. Yep, that's
the tricky part of it,, I believe.

[More Snip]
-Arthur

--
"It is impossible to make anything foolproof
because fools are so ingenious"
- A. Bloch
Nov 14 '05 #25

P: n/a
Profetas <xu*****@yahoo.com> wrote:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks


2**8 = 256. So what you would do is generate a random number
between 0 and 255. A crude way of doing it would be to call
(char)random();

Assuming that you run a x86 computer and char is 8 bits long.
Nov 14 '05 #26

P: n/a
Viktor Lofgren <ro**@eudial.nos--pam.mine.nu> writes:
Profetas <xu*****@yahoo.com> wrote:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks


2**8 = 256. So what you would do is generate a random number
between 0 and 255. A crude way of doing it would be to call
(char)random();

Assuming that you run a x86 computer and char is 8 bits long.


And that char is unsigned, among other assumptions.

The idea was proposed and shot down elsewhere in this thread.

Oddly enough, question 13.16 in the C FAQ is "How can I get random
integers in a certain range?".

<http://www.eskimo.com/~scs/C-faq/top.html>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #27

P: n/a
On Fri, 21 May 2004 20:26:47 GMT, "Mabden" <ma****@sbcglobal.net>
wrote:

[snip]
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. ;-)


The game has 4 billion possible beginnings anyway. I mean, as long as
you're not changing seed values with srand(), your rand()s will
produce only 4 billion different sets of numbers.

Here's a simple pseudo-random number generator in pseudo-code:

number[t] = (M * number[t-1]) % N

number[0] is the seed, and for best results, M and N are prime
numbers. (I'm sure there are other factors as well, but I can't list
them off the top of my head.)

For a C implementation using the method above, I'd say that M is a 10
or 9-digit prime number, and N is 32767, which looks like a Mersenne
prime, but is unfortunetely not.
--
aib

ISP e-mail accounts are good for receiving spam.
Nov 14 '05 #28

P: n/a

In article <Hr*****************@newssvr27.news.prodigy.com> , "Mabden" <ma****@sbcglobal.net> writes:

[using PRNG for a game]


There's some good material in the sci.crypt FAQ on using a PRNG to
shuffle a deck of cards. I recommend looking at that.

--
Michael Wojcik mi************@microfocus.com
Nov 14 '05 #29

P: n/a

In article <Pi***********************************@unix41.andr ew.cmu.edu>, "Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:


srand((unsigned)time(0)) is the "canonical" way to get a random seed
into the PRNG. Of course it's not secure, but it's good enough for
games and things that you won't ever need to run twice in close succession
expecting different outputs.


For sufficently small values of "good enough", perhaps. Games that
use srand((unsigned)time(0)), or for that matter the rand() provided
by many implementations, often have terrible randomizing (shuffling
or what have you). Take card games played with a full deck, for
example - you don't get lg(52!) bits of entropy in your typical
rand() implementation, and srand() certainly doesn't let you set
them. (And many people use lousy shuffling algorithms.)

C simply doesn't provide a good PRNG for most games as part of the
standard library. People who want to write games with a (pseudo-)
random aspect in C should do a bit of research and find out how to
do it correctly.

--
Michael Wojcik mi************@microfocus.com

A coding theorist is someone who doesn't think Alice is crazy. -- John Gordon
Nov 14 '05 #30

P: n/a

In article <56********************@bgtnsc05-news.ops.worldnet.att.net>, Nick Landsberg <hu*****@NOSPAM.att.net> writes:

Many decades ago, I remember that to check for randomness we would
use the Chi-Square test. (Google for it.) But the instructor
insisted that we run enough tests that we could also run a
Chi-Square of the Chi-Square results. The first set of
results should also follow some kind of random pattern
as determined by the second Chi-Square. Yep, that's
the tricky part of it,, I believe.


There's a lot of literature on PRNG quality tests. Knuth goes into
it in some detail, IIRC. George Marsaglia (who occasionally posts
here) maintains the popular Diehard suite of tests for RNGs and PRNGs
(or more precisely, for sequences of values purported to be random or
pseudorandom). You can read about Diehard's tests at [1].

They include tests for value spacings, run lengths, missing values
under various encodings, and so forth.

1. http://stat.fsu.edu/pub/diehard/cdrom/source/tests.txt

--
Michael Wojcik mi************@microfocus.com

Do we have boyfriends? We are interested in delicious food and sweets.
And tiny animals like the cat. -- Naoko Yamano
Nov 14 '05 #31

P: n/a
Michael Wojcik wrote:
In article <56********************@bgtnsc05-news.ops.worldnet.att.net>, Nick Landsberg <hu*****@NOSPAM.att.net> writes:
Many decades ago, I remember that to check for randomness we would
use the Chi-Square test. (Google for it.) But the instructor
insisted that we run enough tests that we could also run a
Chi-Square of the Chi-Square results. The first set of
results should also follow some kind of random pattern
as determined by the second Chi-Square. Yep, that's
the tricky part of it,, I believe.

There's a lot of literature on PRNG quality tests. Knuth goes into
it in some detail, IIRC. George Marsaglia (who occasionally posts
here) maintains the popular Diehard suite of tests for RNGs and PRNGs
(or more precisely, for sequences of values purported to be random or
pseudorandom). You can read about Diehard's tests at [1].

They include tests for value spacings, run lengths, missing values
under various encodings, and so forth.

1. http://stat.fsu.edu/pub/diehard/cdrom/source/tests.txt

That was fascinating reading.
Thanks for the reference.
--
"It is impossible to make anything foolproof
because fools are so ingenious"
- A. Bloch
Nov 14 '05 #32

P: n/a
Orhan Kavrakoglu <ga*******@ttnet.net.tr> writes:
[...]
The game has 4 billion possible beginnings anyway. I mean, as long as
you're not changing seed values with srand(), your rand()s will
produce only 4 billion different sets of numbers.


That's assuming that unsigned int (the type of the argument to
srand()) is 32 bits, and that srand() uses all 32 bits.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #33

P: n/a
Michael Wojcik wrote:
.... snip ...
There's a lot of literature on PRNG quality tests. Knuth goes into
it in some detail, IIRC. George Marsaglia (who occasionally posts
here) maintains the popular Diehard suite of tests for RNGs and PRNGs
(or more precisely, for sequences of values purported to be random or
pseudorandom). You can read about Diehard's tests at [1].

They include tests for value spacings, run lengths, missing values
under various encodings, and so forth.

1. http://stat.fsu.edu/pub/diehard/cdrom/source/tests.txt


Accessing that results in "Forbidden".

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #34

P: n/a
On Sun, 23 May 2004 22:49:51 +0000, CBFalconer wrote:
1. http://stat.fsu.edu/pub/diehard/cdrom/source/tests.txt


Accessing that results in "Forbidden".


I got it just fine. Should I email it to you?

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #35

P: n/a
August Derleth wrote:
On Sun, 23 May 2004 22:49:51 +0000, CBFalconer wrote:
1. http://stat.fsu.edu/pub/diehard/cdrom/source/tests.txt


Accessing that results in "Forbidden".


I got it just fine. Should I email it to you?


That would be pleasant, thanks. At worldnet.att.net.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #36

P: n/a

In article <40***************@yahoo.com>, CBFalconer <cb********@yahoo.com> writes:
Michael Wojcik wrote:

1. http://stat.fsu.edu/pub/diehard/cdrom/source/tests.txt


Accessing that results in "Forbidden".


Interesting. I retrieved it fine with IE, but after I saw your
post I did a quick manual test (piped the GET request into a simple
TCP client, similar to netcat), and did indeed get an HTTP 403
(Forbidden) error.

http://stat.fsu.edu/pub/diehard/cdrom/ is OK, but source/ or anything
under it returns a 403. I'm not sure what IE is doing under the
covers. Ah - a bit of conversation snooping and further experimenta-
tion have provided the answer. The server at stat.fsu.edu will only
serve the document in response to an HTTP/1.1 request, not an
HTTP/1.0 request. (It has to be a well-formed HTTP/1.1 request, with
a Host header as well as the Request-line.)

HTTP/1.1 really is an improvement over HTTP/1.0 in many respects, and
all other things being equal it would be good to use a browser that
supports it. But all other things are not, of course, equal, and I
understand why some people are reluctant to move to a newer browser.
(I have to use IE for work reasons, but I'm not happy with it.)

--
Michael Wojcik mi************@microfocus.com

An intense imaginative activity accompanied by a psychological and moral
passivity is bound eventually to result in a curbing of the growth to
maturity and in consequent artistic repetitiveness and stultification.
-- D. S. Savage
Nov 14 '05 #37

This discussion thread is closed

Replies have been disabled for this discussion.