c - Is there a way to use rand() with a variable? -
i want use rand in loop int variable decrements during each loop.
can
randomnumber = rand()%d, otherintvar;
?
edit: everyone, can put name of integer variable there instead of number , work.
i think you're trying accomplish this:
#include <stdio.h> #define iteration_count 10 // define loop count here #define init_value 100 // define initial value here int main() { int i, num, val; val = init_value; for(i = 0; i<iteration_count; ++i) { num = rand() % val; // yields random number between [0-val] --val; // define how needs decremented // program logic } // program logic return 0; }
however, should add control logic otherwise not results predicted. (set iteration count , decremential rate proportional eachother example.)
Comments
Post a Comment