Need help in recursion program

This is a discussion on Need help in recursion program within the C Programming forums, part of the General Programming Boards category; Hi , I am facing a problem in using recursion. I am creating random number of 6 digit for some ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    Need help in recursion program

    Hi ,
    I am facing a problem in using recursion.
    I am creating random number of 6 digit for some purpose.
    The check I have to made is that the generated random number should be of 6 digit,if it is greater than 6 then I have wrote logic for making it to 6 digit but the problem arises when number is less than 6.
    In that case there can be two scenarios:

    1) I recall that random generator function again
    2) I convert the less digit number say 4 to 6 digit with some logic

    In second case there is over head of writing logic,how to add extra digit.

    So I prefer first case, but there was a problem.
    I have implement a logic that if generated no is < 100000, then call the function again.

    The problem arises when calling recursive function the new generated is less than 6 digit and so on.Due to this stack size is increasing and over head increase to excecute the statement which are useless.

    But with time the probabilty is 8 : 20000 i.e in 20000 iteration , there are 8 cases when no. generated is less than 6.

    I have two questions:
    1) Is this the correct approach ?
    2)If yes,then is there a way where I can skip stack over head.
    3) If no, then what's the correct approach.

    Please consider that the function and seed used in generating is OS specific.
    The normal random generated no.s are usually 9-10 digit but in some cases, it is gving less than 6 digit.plz ask free for queries.

    In laymen language the code is:

    Code:
    seedGenrator( seed );
    long ran =genrateRandom(seed); // seed is clock time with pico seconds
    This code is platform specific so please consider only logical portion.

    Thanks
    Bargi

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why not work only with random numbers within the range that you really want.

    rand % 100, for instance, will give you random numbers in the range of 0 to 99

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    An easy approach would be to alter your recursivefunction to make it work in a different way. You could, for example, do something similar to the code below. Since the function is not recursive the stack will not grow if the number is less then 100 000, you will just generate a new one.

    Code:
    long
    foozbar (void)
    {
      long x;
    
      do {
        x = your_rand();
      } while (x < 100000);
    
       return x;
    }
    edit:

    I would write some logic to always get numbers bigger then 100 000 instead, though. It will be more optimized then relying on some generate_random_number_function to return a number and then check if it is 6 digits or more.
    Last edited by edoceo; 03-17-2009 at 11:51 AM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Adak , Is that you mean that, if I divide random no. with 100000,it will give me always 6 digit no. and more than that.
    Since currenlty I am getting random no. of range 5,6,7,8,9,10,11 digit number.
    And also I have to put logic if the generated no. is 6 digit or less.In that case I have to recall the function or do the logic given by edoceo.
    please clarify..
    Also the number should not repeated.

    Thanks
    Bargi

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Bargi View Post
    Adak , Is that you mean that, if I divide random no. with 100000,it will give me always 6 digit no. and more than that.
    Since currenlty I am getting random no. of range 5,6,7,8,9,10,11 digit number.
    And also I have to put logic if the generated no. is 6 digit or less.In that case I have to recall the function or do the logic given by edoceo.
    please clarify..
    Also the number should not repeated.

    Thanks
    Bargi
    No, % is C language for modulo (remainder) math operator. So 10 % 3 == 1 (10 / 3), but throw away the normal answer, and keep the remainder.

    Then rand % 100 would have a range of 0 to 99.

    Just add the number of zero's you need and there you go - a range of random numbers.

    Now add another number to set the lower limit:

    rand % 2,000,000 + 1,000,000 would give you random numbers in the range of 1,000,000 - 199,999,999. (commas added just for us humans to read, remove them for the computer.

    You'll want unsigned long as your data type, but check your range on your system, with your compiler.

    If you can't repeat a number, then I would generate the numbers first, and put them into a file or array, then just have your program sort the numbers, and read from that file or array. Any duplicated numbers can easily be found because they will be right next to each other, when they're sorted, and such should be removed before your program does anything else with these numbers.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Edeco,
    Actually I have to generate random with unique key.
    Currentky I am using sytem time with date,month,year,hr,min,sec,picosec.
    With this type of seed I always got the unique number.
    That why I have used this logic.
    Can u suggest me the hint but remember the numbers can't repeated.
    The logic tell by adak is not working since numbers are repeating and also less than 6 digit are generating.

    Thnks

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please read #5 post.

    I didn't know you didn't know to add to the random number, to get a "no less than" floor level.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use rand()%900000 and add one hundred thousand.
    Last edited by MK27; 03-17-2009 at 12:46 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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