📄 rand.c
字号:
/*FUNCTION<<rand>>, <<srand>>---pseudo-random numbersINDEX randINDEX srandINDEX rand_rANSI_SYNOPSIS #include <stdlib.h> int rand(void); void srand(unsigned int <[seed]>); int rand_r(unsigned int *<[seed]>);TRAD_SYNOPSIS #include <stdlib.h> int rand(); void srand(<[seed]>) unsigned int <[seed]>; void rand_r(<[seed]>) unsigned int *<[seed]>;DESCRIPTION<<rand>> returns a different integer each time it is called; eachinteger is chosen by an algorithm designed to be unpredictable, sothat you can use <<rand>> when you require a random number.The algorithm depends on a static variable called the ``random seed'';starting with a given value of the random seed always produces thesame sequence of numbers in successive calls to <<rand>>.You can set the random seed using <<srand>>; it does nothing beyondstoring its argument in the static variable used by <<rand>>. You canexploit this to make the pseudo-random sequence less predictable, ifyou wish, by using some other unpredictable value (often the leastsignificant parts of a time-varying value) as the random seed beforebeginning a sequence of calls to <<rand>>; or, if you wish to ensure(for example, while debugging) that successive runs of your programuse the same ``random'' numbers, you can use <<srand>> to set the samerandom seed at the outset.RETURNS<<rand>> returns the next pseudo-random integer in sequence; it is anumber between <<0>> and <<RAND_MAX>> (inclusive).<<srand>> does not return a result.NOTES<<rand>> and <<srand>> are unsafe for multi-thread applications.<<rand_r>> is MT-Safe and should be used instead.PORTABILITY<<rand>> is required by ANSI, but the algorithm for pseudo-randomnumber generation is not specified; therefore, even if you usethe same random seed, you cannot expect the same sequence of resultson two different systems.<<rand>> requires no supporting OS subroutines.*/#ifndef _REENT_ONLY#include <stdlib.h>#include <reent.h>void_DEFUN (srand, (seed), unsigned int seed){ _REENT_RAND_NEXT(_REENT) = seed;}int_DEFUN_VOID (rand){ /* This multiplier was obtained from Knuth, D.E., "The Art of Computer Programming," Vol 2, Seminumerical Algorithms, Third Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */ _REENT_CHECK_RAND48(_REENT); _REENT_RAND_NEXT(_REENT) = _REENT_RAND_NEXT(_REENT) * __extension__ 6364136223846793005LL + 1; return (int)((_REENT_RAND_NEXT(_REENT) >> 32) & RAND_MAX);}#endif /* _REENT_ONLY */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -