random - Trick the randomizer in C -
i want random numbers between 1 10. works, when it's in loop, don't random numbers.
int randomnum; srand ( (unsigned int)time(null) ); randomnum = rand() % 10;
i've been spending hours here , in google looking solution, looks no 1 solved (or maybe didn't search enough). value randomizer depends on seconds (not miliseconds or else, in other programming language) , that's why numbers not random.
in addition, don't want download package c because run code in university labs, , won't allow it.
is there creative solution problem? maybe mathematic functions?
to illustrate sidoh's answer.
#include <stdlib.h> #include <stdio.h> #include <time.h> int main(int argc, char** argv) { int i; srand ( (unsigned int)time(null) ); (i = 0; < 100; i++) { printf("%d ", 1 + (rand() % 10)); } putchar('\n'); return 0; }
this produced following results 1 time seed using time( ).
7 10 2 4 4 4 2 1 7 7 10 4 3 10 2 9 6 9 2 9 7 10 4 1 1 8 2 4 8 1 2 4 2 3 9 5 8 1 7 4 9 8 10 1 8 1 1 5 1 4 5 7 3 9 10 3 6 1 9 3 4 10 8 5 2 7 2 2 9 10 5 9 8 4 1 7 7 2 3 7 5 8 6 10 8 5 4 3 7 2 8 2 1 7 7 5 5 10 6 5
Comments
Post a Comment