2011年7月1日 星期五

C/C++ - rand()

#include<stdlib.h>

if you need a random number between a(min) and b(max) ,
use:

int x = (rand()%(max-min+1))+min

usually use srand() to build random base

ex:
1 int randnum(int min,int max)
2 {
3         int a;
4         srand(time(NULL));
5         a =(rand()%(max-min+1))+min;
6         return a;
7 }

time() include time.h
rand() and srand() include stdlib.h

沒有留言:

張貼留言