Advertisement
cd62131

Random Trials

Dec 11th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 7
  5.  
  6. void print(char *rand, int n) {
  7.   int i;
  8.   printf("randoms: [");
  9.   for (i = 0; i < n; i++) {
  10.     printf("%d", rand[i]);
  11.     if (i < n - 1) printf(", ");
  12.   }
  13.   printf("]\n");
  14. }
  15. int main(void) {
  16.   char randoms[] = { 0, 0, 0, 0, 0, 0, 0 };
  17.   int i, j, trial;
  18.   srand((unsigned int) time(NULL));
  19.   trial = rand() % 7 + 3;
  20.   for (i = 0; i < trial; i++) {
  21.     if (rand() % 2) {
  22.       j = rand() % N;
  23.       randoms[j] = 1;
  24.       print(randoms, N);
  25.       randoms[j] = 0;
  26.     }
  27.     else print(randoms, N);
  28.   }
  29.   return EXIT_SUCCESS;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement