473,836 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Way for computing random primes in standard C.

Hello all,

Is there a function in the standard C library which returns a prime
number which is also pseudo-random?

Assuming there isn't, as it appears from the docs that I have, is there
a better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.

Also what is meant by reinitialising the rand() function with srand(1)?
Does it mean that further calls to rand() will return numbers with a new
starting point? If so, how is it different to calling srand() with a
seed value such as that returned by the time() function?

Thank you all for the help. I find this group very useful.
Feb 24 '06
104 5207
In article <sl************ *******@ID-203069.user.ind ividual.net>,
Pedro Graca <he****@hotpop. com> wrote:
[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.

Gee, I guess that means that "C isn't portable!" ;-)
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Mar 2 '06 #81
On Thu, 02 Mar 2006 21:46:46 GMT, Micah Cowan <mi***@cowan.na me>
wrote:
Al Balmer <al******@att.n et> writes:
On Thu, 02 Mar 2006 21:23:29 GMT, Micah Cowan <mi***@cowan.na me>
wrote:
>Keith Thompson <ks***@mib.or g> writes:
>
>> Micah Cowan <mi***@cowan.na me> writes:
>> > Keith Thompson <ks***@mib.or g> writes:
>> Unless you specifically want to repeat the same sequence of
>> pseudo-random numbers, you should call rand() exactly once, before any
>> calls to rand().
>
>(You meant srand() instead of the first rand())
>
>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.

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.
Then consider the phrase "Unless you
specifically want to repeat the same sequence" above.


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

-Micah


--
Al Balmer
Sun City, AZ
Mar 2 '06 #82
Micah Cowan wrote:

Pedro Graca <he****@dodgeit .com> writes:
[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:Th***** ********@gmail. com>

Mar 2 '06 #83
In article <44************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.net> wrote:
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().
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Mar 2 '06 #84
Al Balmer <al******@att.n et> writes:
On Thu, 02 Mar 2006 21:46:46 GMT, Micah Cowan <mi***@cowan.na me>
wrote:
Al Balmer <al******@att.n et> writes:
On Thu, 02 Mar 2006 21:23:29 GMT, Micah Cowan <mi***@cowan.na me>
wrote:

>Keith Thompson <ks***@mib.or g> writes:
>
>> Micah Cowan <mi***@cowan.na me> writes:
>> > Keith Thompson <ks***@mib.or g> writes:
>> Unless you specifically want to repeat the same sequence of
>> pseudo-random numbers, you should call rand() exactly once, before any
>> calls to rand().
>
>(You meant srand() instead of the first rand())
>
>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.

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).
Mar 2 '06 #85
Micah Cowan <mi***@cowan.na me> writes:
Keith Thompson <ks***@mib.or g> writes:
Micah Cowan <mi***@cowan.na me> writes: [...]
> Yes, he seems to have completely misunderstood you. However, he never
> recommended calling srand() before every rand(), as you've done in
> your example code.
Have you confused me with someone else? I just re-examined everything
I've posted to this thread. I have never recommended calling srand()
before every rand(). (I didn't check all the articles, but I don't
think anyone else did either.) I posted a sample code snippet in
which srand() is called before every 3 calls to rand(), along with a
clear statement that it was only an example, and the number of calls
could be different. That was *not* a recommendation; I was asking Rod
Pemberton whether *he* thought that would be a good idea.


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.

[...]
> I doubt it. But his answer was at least somewhat valid, as he
> advocated calling srand() every once in a while (every shuffle, or
> every 52 calls to rand()), based on a fairly random interval of time
> between srand()s (namely, the time it takes one to go through a deck
> of cards in a card game).


Unless you specifically want to repeat the same sequence of
pseudo-random numbers, you should call rand() exactly once, before any
calls to rand().


(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.
If that doesn't give you good results, the solution
is to use a different pseudo-random number generator, not to call
srand() more than once.


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.

--
Keith Thompson (The_Other_Keit h) 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.
Mar 2 '06 #86
Micah Cowan <mi***@cowan.na me> writes:
Al Balmer <al******@att.n et> writes:
On Thu, 02 Mar 2006 21:23:29 GMT, Micah Cowan <mi***@cowan.na me>
wrote: [...]
>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.

--
Keith Thompson (The_Other_Keit h) 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.
Mar 3 '06 #87
Micah Cowan <mi***@cowan.na me> writes:
[...]
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).

--
Keith Thompson (The_Other_Keit h) 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.
Mar 3 '06 #88
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <44************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.net> wrote:
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().


And C99.

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

--
Keith Thompson (The_Other_Keit h) 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.
Mar 3 '06 #89
Pedro Graca wrote:
[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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Mar 3 '06 #90

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.