Thread: Help regarding random number

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

    Help regarding random number

    Hi ,

    I am working in a problem in which I have to print elements in array using random number logic.

    The logic is that the elements of character should be printed randomly from the array. I have used random function for this.

    The logic I implementing is that, I took the random number using function rand() and pick up the last two digit from it and with respect to them , I took the value from char array and print it down.

    But the problem is that the array size is 0nly 40 and the number I get from generator some times greater than 40,so in this case repeated iterations will be there,which is not good practice.Also, I have to put the logic whether it from unit place or tens place.

    Can anybody help me in implementing this.Actually I want to put the optimized solution.

    The code is as follow:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<time.h>
    
    int main()
    {
      char alpha[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
      int count =0,i,mul=0;
      int seed = (int)time(NULL);
      clrscr();
      srand(seed);
    
      i = rand();
      printf("%d",i);
    
      while(count!=2)
      {
        mul*=10;
        mul+=i%10;
        i = i/10;
        count++;
      }
    
      printf("\n Value=%c",alpha[mul]);
    
      getch();
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Hmm I'm not sure if I understand what it needs to do exactly...

    If you just want the random number to be equal to or below 40 everytime... Put in a loop to keep randing until you get a suitable number (a number less than 40)

    Code:
    while(i%100>=40)
    {
    i=rand();
    }
    I'm not sure if you meant you didn't want to do that with the "repeated iterations" comment but it's fine in terms of programming practice to do it in that fashion.

    Also, I have to put the logic whether it from unit place or tens place.
    I don't quite understand what this means however.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bargi View Post
    The logic I implementing is that, I took the random number using function rand() and pick up the last two digit from it and with respect to them , I took the value from char array and print it down.
    That is so crazy, you may get my first ever "craziest method of the month" award.

    rand() delivers a random number up to the value of RAND_MAX, which is system specific. So, if all you want is a number from 1-6, you divide RAND_MAX by 6, and use that as a divisor on the return value of rand():
    Code:
    int divisor=RAND_MAX/40;
    int random=rand()/divisor;
    I can't remember if rand() starts at 0 or 1, you may have to find some documentation and read it yourself.

    Good luck!

    [edit] actually you can kiss that award goodbye, Sparrowhawk just outdid you by a mile...DO NOT use the Sparrowhawk method. Honestly.
    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

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by MK27 View Post
    That is so crazy, you may get my first ever "craziest method of the month" award.

    rand() delivers a random number up to the value of RAND_MAX, which is system specific. So, if all you want is a number from 1-6, you divide RAND_MAX by 6, and use that as a divisor on the return value of rand():
    Code:
    int divisor=RAND_MAX/40;
    int random=rand()/divisor;
    I can't remember if rand() starts at 0 or 1, you may have to find some documentation and read it yourself.

    Good luck!

    [edit] actually you can kiss that award goodbye, Sparrowhawk just outdid you by a mile...DO NOT use the Sparrowhawk method. Honestly.
    I just saw this, very interesting... Yes you're right, your method is much better. Can I find a list of system specified values like that one somewhere? I'm definitely interesting in knowing all of them now

    Thanks!

    [edit] - To be fair though, it was laziness on my part to read the entire article in the C++ reference... It identifies RAND_MAX and I didn't see that at all... Nevertheless, still interested in finding a listing of these things somewhere!
    Last edited by Sparrowhawk; 03-11-2009 at 12:54 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sparrowhawk View Post
    Can I find a list of system specified values like that one somewhere? I'm definitely interesting in knowing all of them now

    Thanks!
    Absolutely Not. The general protocol with C documentation is to exclude irrelevant details such as this, so that you can spend the afternoon wondering if you are really the dumbest ass in the universe. Then, after an undefined period of having not given up anyway, all those wasted, fretful hours will count as some form of experience with C documentation, so that the first thing you ask yourself when trying to learn something new is: "What aren't they telling me now?"

    I saw an API reference last week that started "You know I wrote this and lots of people use it, so it must work -- but how? By the time you are done here, you might as well have written the whole thing, from scratch, yourself..." I swear it said that.
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bargi
    The logic I implementing is that, I took the random number using function rand() and pick up the last two digit from it and with respect to them , I took the value from char array and print it down.
    You have a right idea, but a wrong implementation. If you really want to do things that way, then just writing rand() % 40 will do. The minor disadvantage is that the distribution would no longer be theoretically uniform.

    Quote Originally Posted by Sparrowhawk
    If you just want the random number to be equal to or below 40 everytime... Put in a loop to keep randing until you get a suitable number (a number less than 40)
    This is also a right idea, but a wrong implementation, since you should then just write:
    Code:
    do
    {
        i = rand();
    }
    while (i >= 40);
    However, [0, 40) is a rather narrow range for this approach. A variant of this approach that works with narrow ranges is to divide [0, RAND_MAX] into 40 equally sized "buckets", with left over numbers left out. The advantage in both cases is that the distribution remains uniform in theory, but then someone once pointed out to me that it is probably overkill anyway.

    Quote Originally Posted by MK27
    rand() delivers a random number up to the value of RAND_MAX, which is system specific. So, if all you want is a number from 1-6, you divide RAND_MAX by 6, and use that as a divisor on the return value of rand():
    This should work, but probably should be changed to:
    Code:
    int random = rand() / (RAND_MAX / 40 + 1);
    with the +1 to accomodate the [0, 40) rather than [0, 40] range. I think it has the same minor disadvantage as with the modulo method.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks For help ......
    Its really solve the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM