1. ## Random Numbers

Is there anyway at all of generating a random number?

The function rand() just generates the name numbers in the same order every single time. For example,

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
int x;
for (x=0;x<30;x++)
{
n=rand();
printf("%d", n);
getchar();
}
return 0;
}```
Will ALWAYS output:

41
18467
6334
26500
19169
15724
etc...

I noticed a tutorial for it, but like basically everything else it is C++*. So is it actually possible in C?

2. Just use srand() to seed the RNG before calling rand() the first time. srand() is usually passed the current time to generate unique values: srand(time(NULL))

3. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int n;
int x;
srand(time(NULL));
for (x=0;x<30;x++)
{
n=rand();
printf("%d", n);
getchar();
}
return 0;
}```

4. Perfect!

Now how do I limit it? Basically I want to randomly generate a number between 0 and 9.

5. Originally Posted by mortalc
Perfect!

Now how do I limit it? Basically I want to randomly generate a number between 0 and 9.
rand() % 10;
rand() returns a very large number, so you use modulo which returns the remainder of the division. So, with modulo 10, the largest possible remainder is 9.

6. Originally Posted by mortalc
Perfect!

Now how do I limit it? Basically I want to randomly generate a number between 0 and 9.
Just use ... n = rand() % 10; ... the number will be 0 to 9 inclusive.

7. So say if I use rand() %11; the highest will be ten.

8. Originally Posted by mortalc
So say if I use rand() %11; the highest will be ten.
Yep. Because if you divide something by 11, you can't get the remainder of 11. Because a remainder of 11 (assuming it would be possible) would mean it divided evenly thus a remainder of zero.

Would it make sense to say 12 / 4 is 2 remainder 4?

9. Try using
Code:
```Include time.h
Then in function
srand(time(NULL));
int x=rand()%11;
// This will provide you unique random numbers most of the times....```

10. This is weird. Here is the main bit of my code:

Code:
```    int sudoku[8][8];
int i;
int a;
int b;
srand(time(NULL));
for(a=0;a<9;a++)
{
for(b=0;b<9;b++)
{
i=rand() %20;
if (i<10 && i!=0)
{
sudoku[a][b]=i;
}
printf("[%2d]", sudoku);
}
}```
It writes [2686476][2686476] etc... A lot of times. I don't know if it is the required 81 or not.
What I want is something like:

[][][][2][6][][8] etc. in 9 rows of 9.

11. Code:
```            i=rand() %20;
if (i<10 && i!=0)
{
sudoku[a][b]=i;```
What happens if your random number from 0 to 19 is not between 1 and 9?

Code:
`               printf("[%2d]", sudoku);`
What is this actually printing?

12. If it is not between 1 and 9, I don't want it to have a value; it is equivalent of the empty box in your sudoku puzzle.

Aha-ha! I should have written
Code:
`printf("[%2d]", sudoku[a][b]);`

13. If you want empty boxes... that is, a certain condition without writing to the array you need to intialize it to these values before beginning... otherwise the array is filled with unknown values that may interfere in the correct solution of the puzzle.
Code:
`sudoku[8][8] = {0};`

14. I am now using this code to create it:
Code:
```int sudoku[8][8] = {0};
int i;
int a;
int b;
srand(time(NULL));
for(a=0;a<9;a++)
{
for(b=0;b<9;b++)
{
i=rand() %20;
if (i<10 && i!=0)
{
sudoku[a][b]=i;
}
printf("[%d]", sudoku[a][b]);
}
printf("\n");
}```
Unfortunately there are a few problems. It will usually create a 9x9, but sometimes it is more of a 20x9. Also, sometimes one or two of the numbers will be absurdly big.

15. You have an off by 1 error: you either want sudoku to be a 9 by 9 array, or you want a < 9 to be a < 8 and b < 9 to be b < 8.