Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

C++ Programming/Code/Standard C Library/Functions/rand

From Wikibooks, open books for an open world
<C++ Programming |Code/Standard C Library |Functions

rand

[edit |edit source]
Syntax
#include<cstdlib>intrand(void);

The function rand() returns a pseudo-random integer between zero andRAND_MAX. An example:

srand(time(NULL));for(i=0;i<10;i++)printf("Random number #%d: %d\n",i,rand());

The rand() function must be seeded before its first call with thesrand() function - otherwise it will consistently return the same numbers when the program is restarted.

Note:
The generation ofrandom numbers is essential tocryptography. Anystochastic process (generation of random numbers) simulated by a computer, however, is not truly random, but pseudorandom; that is, the randomness of a computer is not from random radioactive decay of an unstable chemical isotope, but from predefined stochastic process, this is why this function needs to be seeded.


Related topics
srand
Retrieved from "https://en.wikibooks.org/w/index.php?title=C%2B%2B_Programming/Code/Standard_C_Library/Functions/rand&oldid=3676801"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp