# Thread: Random Number Generator Help

1. ## Random Number Generator Help

I have created a random number generator using this code.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
int i;
int n;
unsigned int seed = (unsigned int)time(NULL);
srand (seed);
for (i = 0; i < 54; i++)
{
n = rand() % 54;
printf ("%d\n", n);
}
return 0;
}```
However, this code gives me duplicates of the same number. How can I resolve this problem? Thanks 2. By duplicates, I take it you mean within a particular run.

If you want to ensure unique numbers for each rand() call, you can create an array 1..53, and each rand() you replace the selected array element with the value at the last element (unless it is the last element), and decrement your mod_by value. 3. If you mean that all 54 numbers generated are not unique, and that is not what you want, then you need to do it a different way.
Start with an array filled with the unique values and use rand to shuffle them.
Then just pick each array index in order. 4. A very quick and effective way to assure you have a list of unique numbers is like this...
Code:
```// card shuffle demonstration

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

#define RANDS 52

int main (void)
{
int Nums[RANDS];  // the array
int item = 0;     // swaps
int temp;
int idx = 0;      // loops

srand(time(NULL));

// fill the array
for (idx = 0; idx < RANDS; idx++)
Nums[idx] = idx + 1;

// unshuffled array
for (idx = 0; idx < RANDS; idx++)
printf("[%d] = %d \t", idx, Nums[idx]);

// shuffle the array
for (idx = RANDS - 1; idx > 0; idx--)
{ item = rand() % idx;
temp = Nums[idx];
Nums[idx] = Nums[item];
Nums[item] = temp; }

// shuffled array
printf("\n\n");
for (idx = 0; idx < RANDS; idx++)
printf("[%d] = %d \t", idx, Nums[idx]);

printf("\n\nPress Enter to exit...");
getchar();
return 0;
}```
Now be smart and don't even think about scoop and poop coding with this example. Study it to learn how it works then WRITE YOUR OWN CODE... (Most schools will check forums like this one and you will just get nailed for cheating.) 5. K guys it works now! Thanks! Here is the finished code.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
int i;
int n;
int temp;
int shuffled;
for (i = 0; i <= 54; i++)
{
shuffled[i] = i + 1;
}
unsigned int seed = (unsigned int)time(NULL);
srand (seed);
for (i = 0; i < 54; i++)
{
n = i + rand() % (54 - i);
temp = shuffled[i];
shuffled[i] = shuffled[n];
shuffled[n] = temp;
}
i = 0;
for (i = 0; i < 54; i++)
{
printf("%d\n", shuffled[i]);
}
return 0;
}``` 6. Err... this results in undefined behaviour: Originally Posted by CommonTater
Code:
`Nums[idx++] = idx;`
I suggest:
Code:
```for (idx = 0; idx < RANDS; idx++)
Nums[idx] = idx;``` 7. oh thanks Tater, I didn't even see your post until I was done, it would have saved me a lot of research! But thanks anyways! 8. Originally Posted by KrazySocoKid
oh thanks Tater, I didn't even see your post until I was done, it would have saved me a lot of research! But thanks anyways!
As CommonTater mentioned, it is good for you to write your own code anyway, plus the research would help you in the long run Use CommonTater's example to see how a more experienced programmer might solve the problem. As for your program, be very careful about writing beyond the bounds of the array: notice that you start with i = 0 but your loop condition is i <= 54. 9. Originally Posted by laserlight Err... this results in undefined behaviour:
How is my while loop undefined behaviour?
I ran that about 30 times and always got exactly the expected results.
idx is changed only once during each iteration of the loop... shouldn't be a problem...

It's no problem to change it in my example, if you can show me the flaw, but I'm not sure I agree with your assessment. 10. Originally Posted by CommonTater
How is my while loop undefined behaviour?
I ran that about 30 times and always got exactly the expected results.
idx is changed only once during each iteration of the loop... shouldn't be a problem...
See comp.lang.c FAQ list · Question 3.1. 11. Originally Posted by laserlight Ahhh... ok, will change the example.

(Ya learn's something new everyday...) Popular pages Recent additions 