C/Math/Random
Содержание
- 1 A random number is chosen between 1 and 100
- 2 A random thought for the day
- 3 Generate a bunch of random data
- 4 Generate random number
- 5 Generate random number: how to use rand
- 6 Initialize random number generator: how to use rand/srand
- 7 Output a random number
- 8 Output random sequence
- 9 Seed rand() with the system time and display the first 10 numbers
- 10 Work with random number
A random number is chosen between 1 and 100
<source lang="cpp">
- include <stdio.h>
- 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">
- include <stdio.h>
- include <time.h>
- 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">
- include <stdlib.h>
- 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">
- include <stdio.h>
- 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">
- include <stdio.h>
- include <stdlib.h>
- 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">
- include <stdio.h>
- include <stdlib.h>
- 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">
- include <stdio.h>
- include "stdlib.h"
int main(void) {
printf("This is a random number: %d", rand()); return 0;
}
</source>
Output random sequence
<source lang="cpp">
- include <stdio.h>
- include <ctype.h>
- include <stdlib.h>
- 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">
- include <stdio.h>
- include <stdlib.h>
- 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">
- include <stdlib.h>
- include <stdio.h>
int main(void) {
int i; for(i=0; i<10; i++) printf("%d ", rand()); return 0;
}
</source>