C/Math/Random

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

A random number is chosen between 1 and 100

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>

int main() {

  int number_to_guess = rand() % 100 + 1;
  printf("%d ",number_to_guess);

}


      </source>


A random thought for the day

<source lang="cpp">

  1. include <stdio.h>
  2. include <time.h>
  3. include <stdlib.h>

void main() {

 char thoughts[][50] = {"W",
                        "A",
                        "M",
                        "T",
                        "A",
                        "A"};

 srand((unsigned int)time(NULL));
 printf("Today"s thought is:\n%s\n", thoughts[rand()%(sizeof thoughts/sizeof thoughts[0])]);

}


      </source>


Generate a bunch of random data

<source lang="cpp">

  1. include <stdlib.h>
  2. include <stdio.h>

const int MAX = 40; int main() {

   int index;
   for (index = 0; index < 20; ++index) {
      int number;
      number = rand();
      printf("%d\n", (number % (MAX-1)) +1);
   }
   return (0);

}


      </source>


Generate random number

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>

int main(void) {

 int magic; /* magic number */
 int guess; /* user"s guess */
 magic = rand(); /* generate the magic number */
 printf("Guess the magic number: ");
 scanf("%d", &guess); 
 if(guess == magic) 
     printf("** Right **");
 return 0;

}


      </source>


Generate random number: how to use rand

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <time.h>

int main () {

 /* initialize random generator */
 srand ( time(NULL) );
 /* generate random numbers */
 printf("RAND_MAX = ", RAND_MAX);
 
 printf ("A number between 0 and RAND_MAX : %d\n", rand());
 
 printf ("A number between 0 and 99: %d\n", rand() % 100);
 
 printf ("A number between 0 and 9: %d\n", rand() % 10);
 return 0;

}

      </source>


Initialize random number generator: how to use rand/srand

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <time.h>

int main () {

 /* initialize random generator */
 srand ( time(NULL) );
 /* generate random numbers */
 printf ("A number between 0 and 100: %d\n", rand() % 100);
 printf ("A number between 20 and 30: %d\n", rand() % 10 + 20);
 return 0;

}

      </source>


Output a random number

<source lang="cpp">

  1. include <stdio.h>
  2. include "stdlib.h"

int main(void) {

 printf("This is a random number: %d", rand());
 return 0;

}


      </source>


Output random sequence

<source lang="cpp">

  1. include <stdio.h>
  2. include <ctype.h>
  3. include <stdlib.h>
  4. include <time.h>

void main() {

  int sequence_length = 0;           
  int i = 0;                         
  long seed = 0;                     
  sequence_length = 2;              
  srand((int)seed);               
  for(i = 1; i <= sequence_length; i++)
     printf("%d ", rand() % 10);  

}


      </source>


Seed rand() with the system time and display the first 10 numbers

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <time.h>

int main(void) {

 int i, stime;
 long ltime;
 /* get the current calendar time */
 ltime = time(NULL);
 stime = (unsigned) ltime/2;
 srand(stime);
 for(i=0; i<10; i++) printf("%d ", rand());
 return 0;

}


      </source>


Work with random number

<source lang="cpp">

  1. include <stdlib.h>
  2. include <stdio.h>

int main(void) {

 int i;
 for(i=0; i<10; i++)
   printf("%d ", rand());
 return 0;

}


      </source>