Random number generation function

Source: Internet
Author: User

Random number generation function

Example:

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

Operation Result:

1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421
1025202362

Run again

1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421
1025202362

Same as above! It is concluded that rand () is pseudo-random, with the same results for each run. So how do you avoid this "pseudo-random" ? That's going to be another function srand () mate,Srand () It means: Place random number seed . As long as the seed is different, rand () produces a different random number.

 #include <stdio.h> #include  <stdlib.h> int   main () { int      a,i; srand (     100  );   for  (I=0 ; I<=10 ;        I++ = rand (); printf (  " %d \ n   "    , a);    } getchar ();  return  0  ;}  

Operation Result:

677741240
611911301
516687479
1039653884
807009856
115325623
1224653905
2083069270
1106860981
922406371
876420180

#include <stdio.h>#include<stdlib.h>intMain () {intA,i; Srand ( Ten);  for(i=0; i<=Ten; i++) {a=rand (); printf ("%d \ n", a);    } getchar (); return 0;}

Results:

1215069295
1311962008
1086128678
385788725
1753820418
394002377
1255532675
906573271
54404747
679162307
131589623

The random number seeds are different and produce the same results.

How to make it every time the generation is not the same, the system time is constantly changing, we can not use it?

#include <stdio.h>#include<stdlib.h>#include <time.h>intMain () {intA,i; unsigned  int TM =Time  (NULL);    Srand (tm);  for(i=0; i<=Ten; i++) {a=rand (); printf ("%d \ n", a);    } getchar (); return 0;}

Run for the first time:

1943618223
1373471778
1476666181
2054163504
937505999
1233927247
1056853368
1812086252
862771187
530774611
1117905961

Run the second time:

969995764
349618453
203599721
151758322
444368239
1714632333
2034185210
145622174
1608107639
1863907432
2110476585

Run the third time:

1637559588
1103816085
742512873
331771740
981048319
2136780304
662571564
380818899
1190877608
2101919912
359796216

The above results are not the same, but the random number generated is relatively large, can you customize the number generated in a range?

#include <stdio.h>#include<stdlib.h>#include<time.h>intMain () {intA,i; unsignedintTM =Time (NULL);    Srand (tm);  for(i=0; i<=Ten; i++) { a = rand ()%101; // generate only random numbers from 0 to 100 printf"%d \ n", a);    } getchar (); return 0;}

Run:

73
26
73
16
3
70
10
37
83
50
40

The range of random numbers is controlled by the method of taking the remainder.

Random number generation function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.