1. ## Random Number Generating

Hi All,

I need your help with Random Number Generating...How can I generate Numbers with a specific length ? I mean...It should ask me: What is the length of the Random Numbers You Want To Generate ?...If I tell 5 for example, then It should generate all Numbers with length 5 digts...

Also...May I know the difference between: rand , random and randomize ?

2. This code does the trick:

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

int main() {
// Set seed for rand() function, if you don't do this the
// numbers will be the same everytime you call the program
srand((unsigned)time(NULL));

unsigned int digits;
printf("Number of digits: ");
scanf("%u",&digits);

unsigned int num = 0;
int i;
for(i = 0; i < digits; i++) {
// Multiplies the original number by ten and then adds
// a random number from 0 - 9 to it
num            = num*10 + (rand() % 10);
}

printf("Random number: %u\n",num);
return 0;
}```
The biggest value of an unsigned int = 2^32 = 4294967296, which is 10 digits long. So that means that when the user wants a number with more than 10 digits, the program will be giving smaller numbers... Test it yourself

3. check out the FAQ for generating random numbers. search the forum, which i suppose you haven't done till now. this is a very common topic

4. >>If I tell 5 for example, then It should generate all Numbers with length 5 digts...
Hmm, interesting problem. I suppose since somebody has already done your work for you, I can show a different solution:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int length, min, max, r;

printf("What length random number? ");
fflush(stdout);
if (scanf("%d", &length) != 1) {
fprintf(stderr, "Input error\n");
return EXIT_FAILURE;
}

/* Find the range */
for (min = 1; --length > 0; min *= 10)
;
max = min * 10;

/* Get a random number in that range */
r = min + rand() / (RAND_MAX / (max - min + 1) + 1);
printf("%d\n", r);

return EXIT_SUCCESS;
}```
>>Also...May I know the difference between: rand , random and randomize ?
random and randomize are the nonstandard equivalents to rand and srand.

5. Originally Posted by Snip
This code does the trick:

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

int main() {
// Set seed for rand() function, if you don't do this the
// numbers will be the same everytime you call the program
srand((unsigned)time(NULL));

unsigned int digits;
printf("Number of digits: ");
scanf("%u",&digits);

unsigned int num = 0;
int i;
for(i = 0; i < digits; i++) {
// Multiplies the original number by ten and then adds
// a random number from 0 - 9 to it
num = num*10 + (rand() % 10);
}

printf("Random number: %u\n",num);
return 0;
}```
The biggest value of an unsigned int = 2^32 = 4294967296, which is 10 digits long. So that means that when the user wants a number with more than 10 digits, the program will be giving smaller numbers... Test it yourself
Thanx Alot Snip for the given code...
The problem with that code is: It may repeat the same numbers..That is, it may generate 33 for example more than one time...So How can I eliminate this ?
Other problem is: How can I get the specifies length ? For example, If I say the number of digits required is 5....So 33 maybe one of the random numbers generated. I don't want to get 33, I want to get 00033 which consists of 5 digits...So How can I solve this extra problem ?
Thanx again Snip...

6. Originally Posted by PING
check out the FAQ for generating random numbers. search the forum, which i suppose you haven't done till now. this is a very common topic
Thanx Alot PING for the advice. Although I have searched the forum without getting a result, I'll try to search again. I think I have not used the suitable search words. Actually I found some basics in the FAQ section.

Thanx again PING...

7. Originally Posted by Sake
>>If I tell 5 for example, then It should generate all Numbers with length 5 digts...
Hmm, interesting problem. I suppose since somebody has already done your work for you, I can show a different solution:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int length, min, max, r;

printf("What length random number? ");
fflush(stdout);
if (scanf("%d", &length) != 1) {
fprintf(stderr, "Input error\n");
return EXIT_FAILURE;
}

/* Find the range */
for (min = 1; --length > 0; min *= 10)
;
max = min * 10;

/* Get a random number in that range */
r = min + rand() / (RAND_MAX / (max - min + 1) + 1);
printf("%d\n", r);

return EXIT_SUCCESS;
}```
>>Also...May I know the difference between: rand , random and randomize ?
random and randomize are the nonstandard equivalents to rand and srand.
Thanx Alot Sake for your effort...It is really nice to have interest in helping others..

Okay the Solution you have given is also working as Snip's solution...The problems with your solution are the same as Snip's solution + 1...The new problem is that I'm getting the same random number each time I run the program...So here I realized the importance of srand function..

Oooh...I forgot to mention another problem with the 2 solutions given: I want the code to generate ALL random numbers with the specific length..NOT only one number, 2 numbers or x numbers...So is that possible ?

Thanx again Sake...

8. Well it doesn't sound like your looking for random numbers at all then. You want all the numbers with a specific number of digits, aka 1 digit would be 0 through 9?

9. Hmm solution for the 00033 problem:

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

int main() {
// Set seed for rand() function, if you don't do this the
// numbers will be the same everytime you call the program
srand((unsigned)time(NULL));

unsigned int digits;
printf("Number of digits: ");
scanf("%u",&digits);

char num[digits + 1];

int i;
for(i = 0; i < digits; i++) {
// Multiplies the original number by ten and then adds
// a random number from 0 - 9 to it
num[i] = (rand() % 10) + '0';
}
num[digits] = '\0';

printf("Random number: %s\n",num);
return 0;
}```

But what do you mean with "I want the code to generate ALL random numbers with the specific length"? Cause in that case you'd have to use a for loop or something... But than it wouldn't be random numbers anymore?

10. >>I forgot to mention another problem with the 2 solutions given
They aren't meant to be complete solutions to your problem, just examples to get you started. We don't do your work for you, but we will offer suggestions that you can use to write your program.

It also helps to specify your problem domain in exact detail before you ask people to go off and come up with code that will help you.