Random Number Generating

This is a discussion on Random Number Generating within the C Programming forums, part of the General Programming Boards category; Hi All, I need your help with Random Number Generating...How can I generate Numbers with a specific length ? I ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    5

    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 ?

    Thanx in advance...

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

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>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.
    Kampai!

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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    Quote 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. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    Quote 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. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  9. #9
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    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. #10
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>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.
    Kampai!

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, 03:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21