/* randtest */ #include #include #include /* In this demo, the drunk starts at position 4, throws a 7 sided dice to decide which way to go in one time unit. If the dice comes up 1,2, or 3 he goes right, and if it comes up 4 or 5 he goes left, and if it comes up 6 or 7 he stands still in that time unit. If he goes right or left, he throws a five sided die to decide how many steps to take in that time unit. */ time_t seed; main() { int i, roll, step, position, endposition; int r; seed = time(NULL); srand(seed); r = rand(); /* This is just to look once at a random number that was generated by rand */ printf("%d\n", r); position = 4; for (i = 1; i <= 20; ++i) { printf("roll = %d\n", roll = 1 + rand()%7); if (roll <= 3) { step = 1 + rand()%5; position = position + step; printf(" step and position = %d %d \n", step, position);} else if ((roll > 3) && (roll <= 5)) position = position - (1 + rand()%5); printf("position = %d\n", position); } endposition = position; printf("The end position for this simulation is %d\n", endposition); }