srand
隨機數發生器的初始化函數
隨機輸出十個0-100之間的整數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include #define MAX 10 int main (void) { int number[MAX] = {0}; int i; unsigned int seed; scanf ("%d",&seed); srand(seed); for(i = 0; i < MAX; i++) { number[i] = rand() % 100; printf("%d ", number[i]); } printf("\n"); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include #include #include #define MAX 10 int main( void ) { int number[MAX] = {0}; int i; srand((unsigned) time(NULL)); for(i = 0; i < MAX; i++) { number[i] = rand() % 100; printf("%d ", number[i]); } printf("\n"); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include #include < ctime > #include stdlib> using namespace std; int main() { double random(double,double); srand(unsigned(time(0))); for(int icnt = 0; icnt != 10; ++icnt) cout << "No." << icnt+1 << ": " << int(random(0,10))<< endl; return 0; } double random(double start , double end) { return start+(end-start)*rand()/(RAND_MAX+ 1.0);} |