rand function

M

Michael Mair

What algorithm does the rand function use?

The C standard does not prescribe one specific algorithm.
The rand function might return 0, 42, and RAND_MAX and then
start anew at 0 and be conforming.
If you want to know the algorithm _your_ implementation uses
for rand, then either have a look at the documentation (if
available) or at the source (if available).
Otherwise, look for newsgroups that discuss or google for
"pseudo random number generator"/PRNG.
Books about algorithms and data structures typically discuss
PRNGs, too.


Cheers
Michael
 
V

Vladimir S. Oka

(e-mail address removed) opined:
What algorithm does the rand function use?

The Standard doesn't say. You may find your implementation documents
it, though. If you're asking because your application is sensitive to
the quality of (pseudo) random numbers, you may be better off
implementing your own with known properties. It's may even be quicker
than testing the implementation provided one.
 
A

Alan

Ian said:
Please don't multi-post, if you want to ask on more than one group,
cross-post.

Excuse my ignorance, but what is the difference between multi-posting and
cross-posting.

Alan
 
N

Nick Keighley

Vladimir said:
(e-mail address removed) opined:

The Standard doesn't say. You may find your implementation documents
it, though. If you're asking because your application is sensitive to
the quality of (pseudo) random numbers, you may be better off
implementing your own with known properties. It's may even be quicker
than testing the implementation provided one.

but don't try and invent your own algorithm! (Unless you *really*
understand
pngs). Try googleing for Mersenne Twister.
 
M

Michael Mair

Alan said:
Excuse my ignorance, but what is the difference between multi-posting and
cross-posting.

Both mean posting the same message to more than one newsgroup.
Multi-Posting: Send n messages to one newsgroup each.
Cross-Posting: Send 1 message to all newsgroups.
The latter has the obvious advantage that all participants can
see all answers, which often decreases unnecessary traffic due
to duplicate answers.
The former has the advantage that not all participants can see
all answers, which sometimes decreases traffic by not spawning a
lengthy non-resolvable discussion between the newsgroups.

Multi-posting is considered impolite as it usually triggers
answers from all newsgroups when one answer would have sufficed.


Cheers
Michael
 
I

Ian Collins

Alan said:
Ian Collins wrote:




Excuse my ignorance, but what is the difference between multi-posting and
cross-posting.
Multi-posting is sending individual messages with the same question to
more than one group.

Cross-posing is sending one message to multiple groups.

The latter is preferable because responses from one group are also
cross-posted to the others.
 
J

John Smith

What algorithm does the rand function use?

Check out linear congruential prngs. Although not suitable for
applications like Monte Carlo simulations and cryptography, this
algorithm is the one implemented in many programming languages.
Here is a simple example. It writes its output to a text file.

/* mixed (c!=0) linear congruential rng */
/* outputs numbers in the range 0-MOD */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MOD 512
#define ROUNDS 100 /* output ROUNDS numbers */

int main(int argc, char *argv[])
{
int idx, cols;
unsigned int s, s1, m, a, c;
FILE *output;

if(argc == 2) /* seed entered on cmd line */
s = atoi(argv[1]);
else /* seeded with system time */
s = ((unsigned int)time(0));

output = fopen("lcgrandom.txt", "w");
/* the effectiveness of the method depends on
choosing "good" values for these constants */
m = 2147483684; /* modulus */
a = 214013; /* multiplier */
c = 2531011; /* increment */

cols = 0;
for(idx = 0; idx < ROUNDS; ++idx)
{
s1 = ((a * s) + c) % m; /* big number */
fprintf(output, " %3u", s1%MOD); /* big number (modMOD) */
s = s1;
++cols;
if(cols == 20) /* 20 columns */
{
fprintf(output, "\n");
cols = 0;
}
}
fclose(output);

printf("output written to lcgrandom.txt\n");

return 0;
}
 
D

Default User

Michael said:
Both mean posting the same message to more than one newsgroup.
Multi-Posting: Send n messages to one newsgroup each.
Cross-Posting: Send 1 message to all newsgroups.
The latter has the obvious advantage that all participants can
see all answers, which often decreases unnecessary traffic due
to duplicate answers.

It also has the advantage that newsreaders can mark the message(s) read
in all subscribed newsgroups.



Brian
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top