Way for computing random primes in standard C.

C

CBFalconer

Pedro said:
[thread hijack]

Does the standard guarantee the output of this program is the
same on all implementations (that have stdlib.h)

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

int main(void) {
printf("%d\n", rand());
srand(42);
printf("%d\n", rand());
return 0;
}

On my system (I don't have a compiler for my other OS here at
home) it outputs these numbers:

1804289383
71876166

No such guarantee. If you want a completely controlled random
generator, supply your own. For an example of this, see the test
program for my hashlib package, which uses a Mersenne Twister
generator. That way I can supply regression tests by comparing
tester output with my own output.

<http://cbfalconer.home.att.net/download/hashlib.zip>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Kenneth Brody

Micah said:
Pedro Graca said:
[thread hijack]

Does the standard guarantee the output of this program is the same on
all implementations (that have stdlib.h)

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

int main(void) {
printf("%d\n", rand());
srand(42);
printf("%d\n", rand());
return 0;
}
[...]

No. It is guaranteed to return the same output each time you run it on
the same implementation... but individual implementations are quite
free in their choice of what to produce.

Does the standard require that rand() return the same sequence each time
a program is run, if srand() is never called?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Al Balmer

No; srand() will repeat the same sequence of pseudo-random numbers,
whether you did it intentionally or stupidly, so I think my original
phrasing is fine.
Perhaps you should not have made it appear to be a comment on what
Keith wrote, then. I thought you intended something more than just a
random injection of a truism.
 
W

Walter Roberson

Does the standard require that rand() return the same sequence each time
a program is run, if srand() is never called?

Yes, neglecting to call srand() is equivilent to using srand(1)
according to the C89 description of srand().
 
M

Micah Cowan

Al Balmer said:
Perhaps you should not have made it appear to be a comment on what
Keith wrote, then. I thought you intended something more than just a
random injection of a truism.

It was a comment on what he wrote. My point is that calling srand()
more than once /can/ have purposes beyond repeating the same sequence
of pseudo-random numbers, contrary to what Keith implied. Is no one on
this thread actually reading before they post? I've been seeing
nothing but constant misreading from every single contributor on this
thread (and yes, that includes me for at least one mistaken
attribution). I only jumped in to point out that people were making
blatantly false assumptions regarding other people's points; but as
this has continued unabated, I don't see the point in following this
thread any more.

I don't agree, generally, with what Rod's suggesting, but I was trying
to at least clarify that his point was rather different from what was
being claimed by others (Sinan still seems confused, at least), and
could actually be good advice in some very limited situations (in
applications such as he described [card games] and with /extremely/
poor PRNGs, or a very long-lived card player ;-) ).

If y'all aren't prepared to carefully read what you're responding to,
you probably shouldn't be responding in the first place. I'll also
note that the level of civility on this thread has been quite poor on
both sides (but not by all contributors).
 
F

Flash Gordon

Pedro said:
[thread hijack]

Does the standard guarantee the output of this program is the same on
all implementations (that have stdlib.h)

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

int main(void) {
printf("%d\n", rand());
srand(42);
printf("%d\n", rand());
return 0;
}

On my system (I don't have a compiler for my other OS here at home)
it outputs these numbers:

1804289383
71876166

No. However, it should produce the same output every time on your
implementation.
 
K

Keith Thompson

Micah Cowan said:
I never said you recommended any such thing. I said that your code did
so. This is apparently wrong, as it is actually Sinan's code I'm
referring to, after checking up on it. So yes, to some degree, I'm
getting you confused.

Ok. What you wrote above could reasonably be interpreted to imply
that I recommended calling srand() before every rand(), and in fact
that's how I interpreted it. I thought the "as you've done" referred
to *recommending* calling srand() rather than to calling srand().
Thanks for the clarification.

[...]
(You meant srand() instead of the first rand())

Yes, acknowledged elsethread.
Calling srand() more than once will not repeat the same sequence of
pseudo-random numbers, unless you stupidly provide it with the same
input as the last time. I'm fairly certain Rod intended that you call
it each time with a time-based value.

If you *want* to repeat the same sequence, calling srand with the same
argument is exactly what you want to do. (For example, you might want
to produce two identical shuffles of a deck of cards, or otherwise
allow a user to re-play the same game of whatever.)

I'm not convinced that Rod has been consistent. I suggest we wait for
him to clarify what he meant.
I don't see how that follows.

It doesn't follow as a matter of rigorous deductive reasoning, but it
seems to me to be a reasonable common-sense inference. Any decent
pseudo-random number generator is going to be designed to work best
when it's allowed to generate a sequence from a starting seed without
interference. If you try to second-guess the algorithm by arbitrarily
perturbing it with additional calls to srand(), there is no reason to
think that the result is going to be any better (whatever "better"
means). If you *know* that re-seeding the algorithm will generate
better results, that implies, I think, that the algorithm is
defective, and you should just use a better one (many are freely
available). A re-seeding scheme that improves one RNG is unlikely to
improve another RNG, so any code that does something like this is
going to be gratuitously non-portable anyway. Using a different RNG
doesn't even sacrifice portability; numerous RNGs are available in
portable C code.

Rod Pemberton seems to disagree with this (though I haven't been able
to figure out what he's really trying to say). He's free to explain
his reasoning if he wants to. He's also free to drop the whole thing.
 
K

Keith Thompson

Micah Cowan said:
Al Balmer said:
[...]
Calling srand() more than once will not repeat the same sequence of
pseudo-random numbers, unless you stupidly provide it with the same
input as the last time. I'm fairly certain Rod intended that you call
it each time with a time-based value.
s/stupidly/intentionally.

No; srand() will repeat the same sequence of pseudo-random numbers,
whether you did it intentionally or stupidly, so I think my original
phrasing is fine.
Then consider the phrase "Unless you
specifically want to repeat the same sequence" above.

Which clearly no one on this thread /does/ want.

There are perfectly legitimate reasons to repeat the same sequence for
some applications, and there's nothing necessarily stupid about doing
so. That's why the standard specifies that behavior. Your
introduction of the word "stupidly" was, shall we say, less than
illuminating to the discussion.
 
K

Keith Thompson

Micah Cowan said:
It was a comment on what he wrote. My point is that calling srand()
more than once /can/ have purposes beyond repeating the same sequence
of pseudo-random numbers, contrary to what Keith implied. Is no one on
this thread actually reading before they post?

I understood what you wrote, and I disagree with it (see my other
posts for more details).
 
K

Keith Thompson

Yes, neglecting to call srand() is equivilent to using srand(1)
according to the C89 description of srand().

And C99.

(You can get your own PDF copy of the C99 standard plus TC1 and TC2;
google "n1124.pdf".)
 
P

Pedro Graca

Walter said:
Pedro Graca <[email protected]> wrote:

Why copy the "Reply-To:" address and not the "From:" one?

It's ok :)
If I didn't want the address public I wouldn't use it; I just found it
strange to see this old address in a new post and thought my
configuration was messed up :)
 
A

Al Balmer

It was a comment on what he wrote. My point is that calling srand()
more than once /can/ have purposes beyond repeating the same sequence
of pseudo-random numbers, contrary to what Keith implied. Is no one on
this thread actually reading before they post?

Don't assume that those who don't agree with you just aren't reading
properly. I understand what Rod (and apparently you) is suggesting, I
just don't agree. I do agree with Keith (and others) that restarting
the sequence is as least as likely to decrease randomness as improve
it. I also agree that if staying with the same seed isn't random
enough for your purposes, then you need a better generator, rather
than repeatedly poking at the one you've got. Calling srand() multiple
times can only complicate the analysis.
 
A

A. Sinan Unur

....

I don't agree, generally, with what Rod's suggesting, but I was trying
to at least clarify that his point was rather different from what was
being claimed by others (Sinan still seems confused, at least), and

What am I confused about?

1) Calling srand multiple times in the program is only appropriate if it
is called with the same value each time so as to repeat a random
sequence (say, if you are running a simulation).

2) Calling srand multiple times with different values, on the other
hand, is a sure sign of confusion.
could actually be good advice in some very limited situations (in
applications such as he described [card games] and with /extremely/
poor PRNGs, or a very long-lived card player ;-) ).

If the RNG is that poor, one should use another one, rather than trying
to play tricks with srand.

The main reason srand exists is to provide repeatable results in
scientific applications.

Sinan
 
A

A. Sinan Unur

Calling srand() more than once will not repeat the same sequence of
pseudo-random numbers, unless you stupidly provide it with the same
input as the last time.

ITYM 'scientifically'. In science, replicability of results is crucial.

Sinan
 
M

Micah Cowan

Keith Thompson said:
It doesn't follow as a matter of rigorous deductive reasoning, but it
seems to me to be a reasonable common-sense inference. Any decent
pseudo-random number generator is going to be designed to work best
when it's allowed to generate a sequence from a starting seed without
interference. If you try to second-guess the algorithm by arbitrarily
perturbing it with additional calls to srand(), there is no reason to
think that the result is going to be any better (whatever "better"
means). If you *know* that re-seeding the algorithm will generate
better results, that implies, I think, that the algorithm is
defective, and you should just use a better one (many are freely
available). A re-seeding scheme that improves one RNG is unlikely to
improve another RNG, so any code that does something like this is
going to be gratuitously non-portable anyway. Using a different RNG
doesn't even sacrifice portability; numerous RNGs are available in
portable C code.

I think the above is extremely well put.
 
M

Micah Cowan

Keith Thompson said:
Micah Cowan said:
Al Balmer said:
[...]
Calling srand() more than once will not repeat the same sequence of
pseudo-random numbers, unless you stupidly provide it with the same
input as the last time. I'm fairly certain Rod intended that you call
it each time with a time-based value.

s/stupidly/intentionally.

No; srand() will repeat the same sequence of pseudo-random numbers,
whether you did it intentionally or stupidly, so I think my original
phrasing is fine.
Then consider the phrase "Unless you
specifically want to repeat the same sequence" above.

Which clearly no one on this thread /does/ want.

There are perfectly legitimate reasons to repeat the same sequence for
some applications, and there's nothing necessarily stupid about doing
so.
Absolutely.

That's why the standard specifies that behavior. Your
introduction of the word "stupidly" was, shall we say, less than
illuminating to the discussion.

What I mean is, it would be stupid to do so in the context we've been
talking about. I'm sorry if it came across otherwise.

To be as clear as possible, what I meant is: if one proposes to call
srand() multiple times throughout a program's lifetime for the purpose
of "improving randomness", then doing so with the same input each time
would be stupid; however, doing so with different inputs wouldn't
necessarily be a terrible idea in some limited situations.

It's obviously not stupid to call srand() with the same input to
reproduce a given sequence, and I did not mean to suggest so.
 
M

Micah Cowan

A. Sinan Unur said:
What am I confused about?

From the posts that I've read, and from your recently posted code
(upstream), you were making a case against calling srand() (or more
accurately, after) each call to rand(). However, to my knowledge,
nobody was making a case /for/ such behavior.
1) Calling srand multiple times in the program is only appropriate if it
is called with the same value each time so as to repeat a random
sequence (say, if you are running a simulation).

I think everyone's agreeing here.
2) Calling srand multiple times with different values, on the other
hand, is a sure sign of confusion.
could actually be good advice in some very limited situations (in
applications such as he described [card games] and with /extremely/
poor PRNGs, or a very long-lived card player ;-) ).

If the RNG is that poor, one should use another one, rather than trying
to play tricks with srand.

Yes, that would be my preference as well. I would personally never do
as Rod has suggested; I was only trying to point out that he wasn't
completely without a point.
The main reason srand exists is to provide repeatable results in
scientific applications.

Hm. Given rand()'s implementation track-record, I can't think of many
scientific applications in which I would be comfortable using it
(though I don't doubt for a moment the truth of what you say).

OTOH, I have made frequent use of it when writing games. :)
 
K

Keith Thompson

Micah Cowan said:
What I mean is, it would be stupid to do so in the context we've been
talking about. I'm sorry if it came across otherwise.

To be as clear as possible, what I meant is: if one proposes to call
srand() multiple times throughout a program's lifetime for the purpose
of "improving randomness", then doing so with the same input each time
would be stupid; however, doing so with different inputs wouldn't
necessarily be a terrible idea in some limited situations.

It's obviously not stupid to call srand() with the same input to
reproduce a given sequence, and I did not mean to suggest so.

Agreed, and again, thanks for the clarification.

Just to be even clearer, I don't recall anyone suggesting that srand()
should be called again with the same seed for the purpose of improving
randomness (though someone may well have have mistakenly thought that
someone else had suggested it).

Which is not intended to suggest that *you* necessarily suggested that
anyone had suggested it, merely that <POOF> oh, crud, there goes my
last brain cell.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top