Last active
July 7, 2020 05:09
-
-
Save drygdryg/107514d78d45fb7784635c3287ccb011 to your computer and use it in GitHub Desktop.
A PRNG experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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