C Tutorial/stdlib.h/rand

Материал из C\C++ эксперт
Перейти к: навигация, поиск

rand

Item Value Header file stdlib.h Declaration int rand(void); Function generates a sequence of pseudorandom numbers. Each time it is called, an integer between zero and RAND_MAX is returned. RAND_MAX will be at least 32,767.


<source lang="cpp">#include <stdlib.h>

 #include <stdio.h>
 int main(void)
 {
   int i;
   for(i=0; i<10; i++){
     printf("%d \n", rand());
   }
   return 0;
 }</source>

Seed a random number

<source lang="cpp">#include <stdio.h>

  1. include <stdlib.h>

int main(){

   int x;

   int seed = 11;
  
   srand(seed);
   puts("Behold! 100 Random Numbers!");
   for(x=0;x<100;x++){
       printf("%d \n", rand());
   }
   return(0);

}</source>

Behold! 100 Random Numbers!
737120851
1813584622
279403949
1005612121
535583813
2076485441
1183398096
520913449
1154512877
1059637032
741048201
1735789551
1316165860
2091205829
608409849
2043917862
358495264
197486820
1978930740
19687262
608908788
411176766
946129446
727061997
2077503173
1575409043
653838784
1321679137
804509431
1206711916
1315236785
1501041106
2061242451
1959636594
472651030
1602552878
492843352
1968585005
378058371
436935333
712273104
364281872
1606570689
1874954196
490475024
696747194
166772383
1367120714
1112737848
32333121
691207997
793647892
340950235
741361510
935019051
427446745
1514759277
1430185376
528113855
855007105
1319659740
1198466770
1992406528
1702459445
1442334882
1720821640
1129825415
1058388889
1258471118
1660062129
1225867189
1550593068
279245380
1224789394
641368484
878709921
1064686765
1555571968
9653544
839812884
1548263274
2015299471
1313206600
821952515
1312932582
446790851
1064401295
655545570
1543054014
1175887620
944409743
1725662249
463822590
16861250
446033920
907172554
1167012078
103393064
633888716
881115352