1. ## rand()

I have written a simple little math game. I only seed the srand() once but out of 25 uses of rand(), I must get 3 or 4 numbers come back 3 or 4 times.

Is there a way to make sure numbers are not duplicated when using the rand(). It doesn't seem so random

2. If u want no duplicates, u can use an array to store numbers generated and if the newly generated number matches any of the numbers in the array then the program can generate a new random number and test it again.

3. >>>
Is there a way to make sure numbers are not duplicated when using the rand(). It doesn't seem so random
<<<

Of course, if you do this, it is also not random! Maybe you've gone wrong somewhere. Post your code.

4. Rand shouldn't be turning up so many duplicates... for example, the following code
Code:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main ()
{
int i;
srand(0);
for (i = 0; i < 20; i++) printf ("%d\n", rand());
printf ("\n\n");

return 0;
}
Produces no duplicates.
Code:
0
262236772
208042609
800207362
428428949
47718728
1793958355
1543227091
1255063701
1245922717
448084510
1841298076
1050803247
489249763
634882132
437249775
192858938
1769723075
2017244079
451954759
There's probably something in your code forcing the numbers to be the same.

5. Unless you're just trying to produce random numbers over a relatively smalle interval (say, 1-20 or so), in which case duplicates are inevitable...

6. here is the code, i only need 40 random numbers. all ideas welcome. i still have a couple things left in to test with

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

main() {

/*** VARIABLE DECLARATIONS ***/
int i,f,s,r;
int correct = 0;
int wrong = 0;

/*** ARRAY DECLARATIONS ***/
int problem[169] [1] ;

/*** initialize variables ***/
f = 0 ; /* first number in equation */
s = 0 ;

system("cls");

for (i=0; i < 169 ; i++)
{
if ( i > -1 && i < 13)
f = 0;
else if ( i > 12 && i < 26)
f = 1;
else if ( i > 25 && i < 39)
f = 2;
else if ( i > 38 && i < 52)
f = 3;
else if ( i > 51 && i < 65)
f = 4;
else if ( i > 64 && i < 78)
f = 5;
else if ( i > 77 && i < 91)
f = 6;
else if ( i > 90 && i < 104)
f = 7;
else if ( i > 103 && i < 117)
f = 8;
else if ( i > 116 && i < 130)
f = 9;
else if ( i > 129 && i < 143)
f = 10;
else if ( i > 142 && i < 156)
f = 11;
else if ( i > 155 && i < 169)
f = 12;

problem[i][0] = f ;

if ((s < 0) || (s > 12))
s=0;

problem[i][1] = s ;
++s;
}/*** end of filling up the array ***/

srand(time(NULL));
do
{
r = rand() / (RAND_MAX / 169 + 1) ;
printf("%d X %d = ", problem[r][0], problem[r][1]);
if (answer == (problem[r][0] * problem[r][1]))
{
system("cls");
++correct;
switch (correct)
{
case 1 :
break;
case 2 :
printf("Don't make it look to easy\n");
break;
case 3 :
printf("Now your starting to cook with gas\n");
break;
case 4 :
printf("I think you realy got the hang of this now\n");
break;
case 5 :
printf("Keep it up, you doing fine\n");
break;
case 6 :
printf("Ride on pardner\n");
break;
case 7 :
break;
case 8 :
printf("GRRREEEEAAAATTTT JJJJOOOOOBBBB\n");
break;
case 9 :
printf("Fantastic\n");
break;
case 10:
printf("The force is with you at this half way point\n");
break;
case 11:
printf("Outstanding\n");
break;
case 12:
break;
case 13:
break;
case 14:
printf("Happy days\n");
break;
case 15:
printf("Good job\n");
break;
case 16:
printf("You rally are smart\n");
break;
case 17:
printf("Scorching\n");
break;
case 18:
break;
case 19:
printf("Just one more like this one genuis\n");
break;
case 20:
printf(" CONGRATULATIONS\n\n");
printf("\tooo o ooo\n");
printf("\to o o o o\n");
printf("\tooo o ooo\n\n");
printf("\to o\n");
printf("\t o o\n");
printf("\t o o\n");
printf("\t o o\n");
printf("\t o o\n");
printf("\t o o\n");
printf("\t o o\n");
printf("\t o o\n");
printf("\t o\n");
break;
default :
printf ( " help i'm broken and can't get fixed");
break;
}
}
else
{
system("cls");
++wrong ;
switch (wrong)
{
case 1:
printf ("Stop and think about it\n");
break;
case 2:
printf ("Does your one brain cell get lonely\n");
break;
case 3:
break;
case 4:
printf ("My grandma can do math better than you\n");
break;
case 5:
printf ("It's really starting to smell in here\n");
break;
case 6:
printf ("Your good at math, (NOT)\n");
break;
case 7:
printf ("Sorry try again\n");
break;
case 8:
printf ("Look, a fireball crashing to earth\n");
break;
case 9:
printf ("Lost again I see\n");
break;
case 10:
break;
case 11:
break;
case 12:
printf ("Take your shoes off, maybe it will help\n");
break;
case 13:
printf ("I'll bet you can't even remember your name\n");
break;
case 14:
printf ("Go and get some more schooling\n");
break;
case 15:
printf ("Do you need some extra fingers or toes\n");
break;
case 16:
printf ("How does it feel to be wrong this much\n");
break;
case 17:
printf ("Look ma, no brains\n");
break;
case 18:
printf ("One foot in the grave now\n");
break;
case 19:
break;
case 20:
printf ("\t\t\tI WIN\n\n\n\n");
break;
default:
printf ("HELP I'M BROKEN");
break;
}
}
}while ((correct != 20) && (wrong != 20));

return;
}

7. oops the post above is mine , i forgot to log in.

8. try something like this:

Code:
#include <stdio.h>
#include <time.h>

int main(void)
{
int array[21] = { NULL };
int i, num;

srand((unsigned)time(NULL));

for( i = 0; i < 20; ++i)
{
do
{
num = (rand() % 20) + 1;
}
while(array[num]);

array[num] = 1;

printf("%d ", num);
}
return 0;
}
the random number is used as an index for an array of int. when rand generates a number, that element of the array is set to 1 so that if the same number is generated again, the do-while will keep looping until rand() returns a new number.

hope this helps.

hmm, nutshell pretty much gave the same answer. oh, well. my reply can be an example to back it up.

9. Thanks all, got it now. I changed the code and got rid of the arrays all together.

srand ((unsigned)time(NULL));
do
{
f = (rand() %12) + 1 ;
s = (rand() %12) + 1 ;
printf("\n%d X %d = ", f, s);
scanf("%d", &a);
if (a == f * s)