Thread: Random Number Generating

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    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
    Last edited by Snip; 01-30-2005 at 07:22 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    Quote 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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM
  2. Random Number: Mersenne Twister Source Needed
    By mercury529 in forum C++ Programming
    Replies: 12
    Last Post: 11-26-2006, 11:40 AM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM