Skip to content

Instantly share code, notes, and snippets.

@drygdryg
Last active July 7, 2020 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drygdryg/107514d78d45fb7784635c3287ccb011 to your computer and use it in GitHub Desktop.
Save drygdryg/107514d78d45fb7784635c3287ccb011 to your computer and use it in GitHub Desktop.
A PRNG experiment
#include <stdio.h> /* printf, scanf */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
void manualSeed (void) {
int seed;
printf("Введите seed: ");
fflush(stdout);
scanf("%d", &seed);
srand(seed);
int r = rand();
while (r > 10000000) r = r / 10;
printf("rand = %d\n", r);
}
void timeSeed (void) {
time_t t;
time(&t);
int int_time = (int) t;
printf("Текущее время в формате UNIX: %d\n", int_time);
for(int seed = int_time - 30; seed < int_time + 30; seed++) {
srand(seed);
int r = rand();
while (r > 10000000) r = r / 10;
printf("seed = %d rand = %d\n", seed, r);
}
}
void rangeSeed (void) {
for(int seed = 1; seed < 1000; seed++) {
srand(seed);
int r = rand();
while (r > 10000000) r = r / 10;
printf("seed = %d rand = %d\n", seed, r);
}
}
int main (void) {
int choice;
printf("Выберите действие:\n1) Указать seed 2) Использовать в качестве seed ближайшее время 3) 0 < seed < 1000\n> ");
fflush(stdout);
scanf("%d", &choice);
switch (choice) {
case 1:
manualSeed();
break;
case 2:
timeSeed();
break;
case 3:
rangeSeed();
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment