# Thread: Random numbers not random?

1. ## Random numbers not random?

Ok so I have my random generator set within a loop that runs 10 times for my game. How come every time it loops back it generates the same random numbers for each loop. I want it to play 10 DIFFERENT games, by different I mean the players dice should not roll the same values each time.

Code:
```/*

My name is Jack Trocinski

*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int diceI, diceII, S, dicesum, iwin=0, ilose=0, iwinII=0, iloseII=0, iloseIIpoints=0, iwinIIpoints=0;
int l;
int pointin, points=0;

printf("CRAPS\n");
printf("INSTRUCTIONS: Press S then Enter to begin the game.\n\n");

scanf("%d", &S);

for(l = 0; l < 10; l++)
{

printf("Start of game %d\n", l);

srand(time(NULL));
diceI = rand()%(6 - 1 + 1) + 1;
printf ("You rolled a %d ", diceI);
diceII = rand()%(6 - 1 + 1) + 1;
printf ("and a %d. \n", diceII);

dicesum = diceI + diceII;
pointin = dicesum;

if (dicesum == 7) {
printf("You win!\n\n");
iwin = iwin + 1;
}
else if (dicesum == 11) {
printf("You win!\n\n");
iwin = iwin + 1;
}
else if (dicesum == 2) {
printf("You lose.\n\n");
ilose = ilose + 1;
}
else if (dicesum == 3) {
printf("You lose.\n\n");
ilose = ilose + 1;
}
else if (dicesum == 12) {
printf("You lose.\n\n");
ilose = ilose + 1;
}
else
{
while (1) {
diceI = rand()%(6 - 1 + 1) + 1;
printf ("You rolled a %d ", diceI);
diceII = rand()%(6 - 1 + 1) + 1;
printf ("and a %d. \n", diceII);
dicesum = diceI + diceII;

points = dicesum + points;

if (dicesum == 7) {
iloseIIpoints = pointin + points;
printf("You lose. You have earned %d points.\n\n", iloseIIpoints);
iloseII = iloseII + 1;
break;
}
else if (points >= pointin) {
iwinIIpoints = points + pointin;
printf("You win! You have earned %d points.\n\n", iwinIIpoints);
iwinII = iwinII + 1;
break;
}
else continue;

}

}

}

printf("On your first roll you won %d times.\n", iwin);
printf("On your first roll you lost %d times.\n", ilose);
printf("On your second roll you won %d times and earned %d points.\n", iwinII, iwinIIpoints);
printf("On your second roll you lost %d times and earned %d points.\n", iloseII, iloseIIpoints);

return 0;
}```

2. srand(time(NULL));
you should only do this once, try moving it outside of the loop

3. Originally Posted by ಠ_ಠ
you should only do this once, try moving it outside of the loop
Thanks ಠ_ಠ! Works now.

4. Code:
```    srand(time(NULL));
diceI = rand()%(6 - 1 + 1) + 1;```
Yes, you should move the seed out of the loop.
Also (rand() % 6) + 1 ; should be adequate. The extra really doesn't do anything.

5. isn't this expressioin going to give you the same value 6?

6 - 1 + 1 == 6 ??

ssharish

6. Originally Posted by ssharish2005
isn't this expressioin going to give you the same value 6?

6 - 1 + 1 == 6 ??

ssharish
Yep, changin' it now.

7. Cool, got the code to work now. Thanks!