Sponsored Content
Full Discussion: rand()
Top Forums Programming rand() Post 28035 by Perderabo on Wednesday 11th of September 2002 01:37:44 PM
Old 09-11-2002
The man page for rand says that it is a "multiplicative congruential" random number generator. That algorithm yields roughly a uniform distribution, but it's a lousy implementation of a lousy algorithm. It certainly does not produce anything at all like a normal distribution.

You should also have a function called drand48 which is a "linear congruential" random number generator. This also yields a uniform distribution, but the algorithm and implementation are much better than rand.

rand really should not be used in new code.

But unix doesn't have random number generators for other distributions. If you need a normal distribution maybe you can find some freeware by searching on google.

Also, you may want to read "The Art of Computer Programming Volume 2 Seminumerical Algorithms" by Donald Knuth. It shows how to transform uniform random numbers into random numbers with other distributions including normal.
 

We Also Found This Discussion For You

1. Programming

fork and rand()

Hi, I want build 10 processus with fork and that each processus write a value betwen 0 and 9 , but each processus send the same value . So my code ,you can compile and try . #include <time.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ipc.h> ... (7 Replies)
Discussion started by: carton99
7 Replies
rand(3C)																  rand(3C)

NAME
rand(), rand_r(), srand() - simple random-number generator SYNOPSIS
DESCRIPTION
uses a multiplicative, congruential, random-number generator with period 2^32 that returns successive pseudo-random numbers in the range from 0 to 2^15-1. can be called at any time to reset the random-number generator to a random starting point. The generator is initially seeded with a value of 1. returns a random number at the address pointed to by the randval parameter. The seed parameter can be set at any time to start the random- number generator at an arbitrary point. Note The spectral properties of leave a great deal to be desired. provides a much better, though more elaborate, random-number generator (see drand48(3C)). RETURN VALUE
If seed or randval is NULL, returns 0. Otherwise, returns a psuedo-random integer. EXAMPLES
The following: int x, y; srand(10); x = rand(); y = rand(); would produce the same results as: int x, y, s = 10; x=rand_r(&s); y=rand_r(&s); WARNINGS
Users of should note that rand_r() now conforms with POSIX.1c. The old prototype of is supported for compatibility with existing DCE applications only. SEE ALSO
drand48(3C), random(3M), thread_safety(5), random(7). STANDARDS CONFORMANCE
rand(3C)
All times are GMT -4. The time now is 05:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy