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...