Marco Monacelli
Snippets of the day: Random Number in C++ 
Tuesday, July 22, 2008, 12:08 PM
Posted by Administrator
SO: Microsoft Windows\Posix
Type: Random Number
Language: C++
Title: Random Number in C++

To generate random number in C\C++ we can use the functions rand() and srand().

The first function generate a pseudo\random number and srand set the start of random generation, this information are a integer.

WARNING: if you use only rand function the result of the function are predictable. A good example are this.

#include <stdio.h>
#include <strlib.h>
#include <time.h>

int main()
{
srand(time(0));
int rand1 = rand() % 10; //random number da 0 - 9
return 0;
}

add comment   |  permalink   |   ( 3 / 536 )

<Back | 1 | 2 | 3 | Next> Last>>