| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- pseudorandom number functions
#include <stdlib.h>longrandom(void);
voidsrandom(unsigned intseed);
char *initstate(unsigned intseed,char*state,size_tsize);
char *setstate(const char *state);
Therandom() function uses a nonlinear additive feedback random-number generator employing adefault state array size of 31 long integers to return successive pseudo-randomnumbers in the range from 0 to 231 -1. The period ofthis random-number generator is approximately 16 x (231 -1). The size of thestate array determines the period of the random-number generator. Increasing the statearray size increases the period.
Thesrandom() function initializes the current state array using the value ofseed.
Therandom() andsrandom() functions have (almost) the same calling sequence andinitialization properties asrand() andsrand() (seerand(3C)). The difference is thatrand(3C) produces a much less random sequence—in fact, the low dozen bitsgenerated by rand go through a cyclic pattern. All the bits generated byrandom() are usable.
The algorithm fromrand() is used bysrandom() to generate the 31state integers. Because of this, differentsrandom() seeds often produce, within anoffset, the same sequence of low order bits fromrandom(). If low orderbits are used directly,random() should be initialized withsetstate() using highquality random values.
Unlikesrand(),srandom() does not return the old seed because the amountof state information used is much more than a single word. Twoother routines are provided to deal with restarting/changing random number generators. With256 bytes of state information, the period of the random-number generator is greaterthan 269, which should be sufficient for most purposes.
Likerand(3C),random() produces by default a sequence of numbers that canbe duplicated by callingsrandom() with 1 as the seed.
Theinitstate() andsetstate() functions handle restarting and changing random-number generators. Theinitstate() function allows a state array, pointed to by thestateargument, to be initialized for future use. Thesize argument, which specifies thesize in bytes of the state array, is used byinitstate() todecide what type of random-number generator to use; the larger the statearray, the more random the numbers. Values for the amount ofstate information are 8, 32, 64, 128, and 256 bytes. Other valuesgreater than 8 bytes are rounded down to the nearest one ofthese values. For values smaller than 8,random() uses a simplelinear congruential random number generator. Theseed argument specifies a starting pointfor the random-number sequence and provides for restarting at the same point. Theinitstate() function returns a pointer to the previous state informationarray.
Ifinitstate() has not been called, thenrandom() behaves as thoughinitstate()had been called withseed = 1 andsize = 128.
Ifinitstate() is called withsize < 8, thenrandom() uses a simple linearcongruential random number generator.
Once a state has been initialized,setstate() allows switching between state arrays.The array defined by thestate argument is used for further random-numbergeneration untilinitstate() is called orsetstate() is called again. Thesetstate() functionreturns a pointer to the previous state array.
Therandom() function returns the generated pseudo-random number.
Thesrandom() function returns no value.
Upon successful completion,initstate() andsetstate() return a pointer to the previousstate array. Otherwise, a null pointer is returned.
No errors are defined.
After initialization, a state array can be restarted at a different pointin one of two ways:
Theinitstate() function can be used, with the desired seed, state array, and size of the array.
Thesetstate() function, with the desired state, can be used, followed bysrandom() with the desired seed. The advantage of using both of these functions is that the size of the state array does not have to be saved once it is initialized.
Programmers should use/dev/urandom or/dev/random for most random-number generation, especially forcryptographic purposes. Seerandom(7D).
Example 1 Initialize an array.
The following example demonstrates the use ofinitstate() to initialize an array.It also demonstrates how to initialize an array and pass it tosetstate().
# include <stdlib.h>static unsigned int state0[32];static unsigned int state1[32] = { 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 0x27fb47b9 };main() { unsigned seed; int n; seed = 1; n = 128; (void)initstate(seed, (char *)state0, n); printf("random() = %d0\n", random()); (void)setstate((char *)state1); printf("random() = %d0\n", random());}Seeattributes(5) for descriptions of the following attributes:
|
drand48(3C),rand(3C),attributes(5),standards(5),random(7D)
Therandom() andsrandom() functions are unsafe in multithreaded applications.
Use of these functions in multithreaded applications is unsupported.
Forinitstate() andsetstate(), thestate argument must be aligned on anint boundary.
Newer and better performing random number generators such asaddrans() andlcrans()are available with the Oracle Solaris Studio compilers.
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |