rand and srand

  • Thread starter Bill Cunningham
  • Start date
S

santosh

CBFalconer said:
I knew it was deprecated (in fact gone) in C99, but recently found
a .pdf of a draft for C95, and noticed it was also eliminated
there. I assumed this was a mistake, because it was a draft.

The C95 draft was n1124.pdf, found at:

http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf

What? Isn't this a "C99 draft"?
Does anybody know the true facts (about C95). And is there a
descriptive file about the contents of that directory?

Maybe you should ask in comp.std.c.
 
K

Keith Thompson

I wrote the above. Please don't snip attribution lines.
------

void srand (unsigned n)

Initailizes the random number generator with the seed n. After this function
has been called calls to rand ( ) generate a new sequence of random numbers

-- "C pocket reference" by O'reilly

Maybe I misunderstood this. I thought the RAND_MAX macro went with srand
( ). Maybe then it goes to rand ( ).

You're using two functions, srand() and rand(). You need to read the
documentation for both of them. You'll find that rand() returns
values in the range 0 to RAND_MAX.

More specifically, if you call rand() repeatedly, you'll get a
sequence of pseudo-random numbers, each in the range 0 to RAND_MAX.
(You can't change this range; if you want a different range, you have
to take what rand() gives you and then manipulate it somehow.)

The "seed" value set by srand() controls *which* sequence rand() will
give you. The manner in which it does so is unspecified; all you know
is that calling srand() with the same seed will give you the same
sequence, and *not* calling srand() is equivalent to calling srand(1).

A "seed" doesn't specify the range of the random numbers, it affects
the sequence.
 
K

Keith Thompson

CBFalconer said:
I knew it was deprecated (in fact gone) in C99, but recently found
a .pdf of a draft for C95, and noticed it was also eliminated
there. I assumed this was a mistake, because it was a draft.

The C95 draft was n1124.pdf, found at:

http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf

Does anybody know the true facts (about C95). And is there a
descriptive file about the contents of that directory?

No, n1124.pdf is *not* a C95 draft; it's a post-C99 draft, published
in 2005. It incorporates the C99 standard plus TC1 and TC2 (and is
now superseded by n1256.pdf, which additionally incorporates TC3).

I have a PDF copy of the 1995 amendment
(ansi+iso+9899-1990+am1-1995.pdf, title "ANSI/ISO/IEC 9899-1990/AM
1-1995"); there doesn't seem to be an integrated document that folds
this into the C90 standard.

I just checked, and the 1995 amendment does not deprecate implicit
int. Almost all of the amendment affects the standard library; the
only changes to the language proper (section 6) are the addition of
digraphs (<:, :>, <%, %>, %:, %:%:) and the update of __STDC_VERSION__
to 199409L.
 
C

CBFalconer

Keith said:
.... snip ...

No, n1124.pdf is *not* a C95 draft; it's a post-C99 draft, published
in 2005. It incorporates the C99 standard plus TC1 and TC2 (and is
now superseded by n1256.pdf, which additionally incorporates TC3).

I have a PDF copy of the 1995 amendment
(ansi+iso+9899-1990+am1-1995.pdf, title "ANSI/ISO/IEC 9899-1990/AM
1-1995"); there doesn't seem to be an integrated document that folds
this into the C90 standard.

If I had been alert I would have known. N1124 is obviously later
than N869.

So I still don't have a proper description of c90 or C95, nor C89
(that standard is a copy of something I got from Dan Pop, which has
obvious holes in it). Bah.
 
A

Antoninus Twink

No, n1124.pdf is *not* a C95 draft; it's a post-C99 draft, published
in 2005. It incorporates the C99 standard plus TC1 and TC2 (and is
now superseded by n1256.pdf, which additionally incorporates TC3).

Amazing - CBF really is just a silly old fool blundering his way through
this newsgroup. He quotes the C Standard like a fundamentalist quotes
the Bible, but it turns out he doesn't have a clue what version of his
precious Standard is what. It's simply laughable.
 
G

Gerry Ford

Antoninus Twink said:
Amazing - CBF really is just a silly old fool blundering his way through
this newsgroup. He quotes the C Standard like a fundamentalist quotes
the Bible, but it turns out he doesn't have a clue what version of his
precious Standard is what. It's simply laughable.
n1124.pdf is an excellent reference, available for nada for persons whose
egg cost have risen 60% in six months.

Maybe the falconer lays the eggs and has been stopped up recently. I know
his buddy Keith knows more than you, I, and a baseball team about the C
standard.
 
S

santosh

pete said:
n1124 has to do with the next standard.
n1124 is not a draft of any published standard.

Well, I guess I should have been more pedantic. It's a post C99 working
draft including TC1 and TC2, now superseded by n1256.

In any case my point standards. CBFalconer's assertion about n1124 is
wide of the mark. His (bzip compressed) version of n869 is closer to
what he thought n1124 was.
 
K

Kenny McCormack

Well, I guess I should have been more pedantic. It's a post C99 working
draft including TC1 and TC2, now superseded by n1256.

In any case my point standards. CBFalconer's assertion about n1124 is
wide of the mark. His (bzip compressed) version of n869 is closer to
what he thought n1124 was.

IOW: Well, it's Monday and you know what that means. It means CBF has
made another asinine post.
 
R

Richard

Antoninus Twink said:
Amazing - CBF really is just a silly old fool blundering his way through
this newsgroup. He quotes the C Standard like a fundamentalist quotes
the Bible, but it turns out he doesn't have a clue what version of his
precious Standard is what. It's simply laughable.

It was abundantly clear to anyone with half a clue a long time ago.
 
R

Richard

CBFalconer said:
Don't snip references (the initial lines that say "joe wrote") for
any material you quote.

Such documentation is all very well for rough reminders. For
accuracy in the C library, use the C standard. There are a couple
of version listed below. The .bz2 version is pure text and needs
decompression after you get it, the other is a pdf.

Listed where below?
 
M

Mark McIntyre

CBFalconer said:
Mark McIntyre wrote:
... snip ...

I knew it was deprecated (in fact gone) in C99, but recently found
a .pdf of a draft for C95, and noticed it was also eliminated
there. I assumed this was a mistake, because it was a draft.

Does anybody know the true facts (about C95).

As far as I recall, it was deprecated but not disallowed in C95.
 
M

Micah Cowan

You could do that too, I suppose. However, where lots of randomness
is not needed (a CGI application that returns random images), I've
actually just skipped a step and applied the modulus to the system
time.

The most obvious example where naïve pseudo-random number generators
can pose a problem with poor "lower-order bit randomness", is when you
are choosing between two values.

The following example, on some very simple pseudo-random number
generators, will alternate which hand the ball is in on every guess.



#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#if 1 /* <-- set to 1 to use a naive rand() implementation
(based on the example in the standard, except even more
naive). */
#define rand my_rand
#define srand my_srand

static unsigned int my_rand_state = 1;

int rand(void)
{
my_rand_state = (my_rand_state * 1103515245 + 12345) % 32768;
return (unsigned int)my_rand_state;
}

void srand(unsigned int seed)
{
my_rand_state = seed;
}
#endif /* naive rand */

enum {
LEFT_HAND = 'l',
RIGHT_HAND = 'r',
QUIT_REQUEST = 'q'
};

struct hand {
int letter;
const char *name;
};

const struct hand hands[] = {
{ LEFT_HAND, "left" },
{ RIGHT_HAND, "right" }
};

void
do_seed(void)
{
srand((unsigned int)time(NULL));
}

int
get_response(void)
{
int ret, c;

fputs("> ", stdout);
fflush(stdout);

c = ret = tolower(getchar());
while (c != '\n') {
if (c == EOF)
return QUIT_REQUEST;
c = getchar();
}
return ret;
}

int
main(void)
{
int i;
do_seed();

for (;;) {
struct hand h = hands[ rand() % 2 ];
int r;

puts("Guess which hand the ball is in.");
r = get_response();
if (r == QUIT_REQUEST)
return 0;
else if (r != LEFT_HAND && r != RIGHT_HAND) {
puts("Please enter (L)eft, (R)ight or (Q)uit.");
continue;
}

if (r == h.letter)
fputs("You got it! ", stdout);
else
fputs("Nope! ", stdout);

printf("It was in the %s hand.\n", h.name);
}

/* Never reached. */
}
 
K

Keith Thompson

Mark McIntyre said:
As far as I recall, it was deprecated but not disallowed in C95.

The "draft for C95" Chuck mentioned was in fact a post-C99 draft.
C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.
 
M

MisterE

Additionally, if you want to generate a random number between 0 and
2000, the code to do that uses the modulus operator, thusly:

printf("%u\n", (rand() % 2001));

That would only work if RAND_MAX is equally divisible via 2001, otherwise
this would return biased values. If the remained of RAND_MAX by 2001 is not
zero, then values upto the modulous appear one more time than those past it.

The best way which avoid multiplication and division is to take the higher
order bits. The top 11 bits makes a number 0 - 2095. If you get 2001 or
above, ignore it and get another random number.
 
F

Falcon Kirtaran

MisterE said:
That would only work if RAND_MAX is equally divisible via 2001, otherwise
this would return biased values. If the remained of RAND_MAX by 2001 is not
zero, then values upto the modulous appear one more time than those past it.

The best way which avoid multiplication and division is to take the higher
order bits. The top 11 bits makes a number 0 - 2095. If you get 2001 or
above, ignore it and get another random number.

It is improbable, but theoretically that program could thrash rand() to
infinity.
 
M

Mark McIntyre

Keith said:
The "draft for C95" Chuck mentioned was in fact a post-C99 draft.

I was aware of that. I made my comment independently.
C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.

Ok, fair enough. I had a recollection of supposed-c89 comppliant
compilers warning about it, but they were probably written by a Certain
Software Giant.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top