Thread: Random Numbers (Advanced)

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Random Numbers (Advanced)

    say i wanted to do something at random about 20 percent of the time.
    the best way i figure is to pick a random number between 0 and 100. pick a range between
    0 and 100. the difference in the range is the percent. 20 and 40 is 20 percent;
    if the random number is within this range, then "this is the 20% percent of the time".


    because what i just typed doesn't make much sense, say i was creating a texas hold'em poker game for example. typically, most players raise before the flop with Ace-King. But to throw people off, some players don't do this and just call. I want to 'just call' about 20 percent of the time and at random. I want to figure out when "20 percent of the time" is.
    (btw, i'm not creating a poker game. i've just been thinking about it. that's a little out of my league right now)

    1. do you agree with the logic?
    2. is there a more elegant way to do this?
    3. should this be in the AI section?

    Code:
    void main()
    {
    	if( between(  GetRand(0, 100), 1, 20 )  )
    		dontRaise();        
    	
    	return void; // :-P
    }
    
    bool between(int n, int x, int y)
    {
    	int temp;
    
    	//min-max
    	if(x > y)
    	{
    		temp = x;
    		x = y;
    		y = temp;
    	}
    
    	if(n >= x && n <= y)
    		return true;
    	return false;
    }
    
    //taken from FAQ
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
    	
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    452 posts and still using void main()?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Shakti
    452 posts and still using void main()?
    look at my return value
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    look at my errors:
    MinGW studio:
    main.cpp:6: error: `main' must return `int'
    main.cpp:7: error: syntax error before `;' token

    MSVC:
    error C2062: type 'void' unexpected

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Shakti
    look at my errors:
    MinGW studio: main.cpp:7: error: syntax error before `;' token
    MSVC: error C2062: type 'void' unexpected

    *shakes head*

    return void; // :-P (tongue face)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Shakti
    452 posts and still using void main()?
    Old habits die hard!
    #undef _sense_of_humor!
    Last edited by Micko; 03-24-2005 at 03:45 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Micko
    Old habits die hard!
    *shakes head again*

    it's not a habit. i never even used void main. i never could. it was just a little joke tossed in. return void? come on.



    i got edited on en poste
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    From Dev-Cpp (MingW port of GCC)
    2 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp `main' must return `int'
    *shakes head*

    http://www.research.att.com/~bs/bs_faq2.html#void-main

    to do something at random about 20% of the time: each time you get to that point, get a random number in the range 1..100. if the number you picked was in the range 1..20, then do whatever it is you wanted to do.

    try something like this:
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    
    int main()
    {
        int num=0;
        srand(static_cast<unsigned int>(time(0)));
    
        for(register int i=0;i<100;i++)
        {
            if(i%10==0)
            {
                std::cout<<'\n';
            }
            
            if(1+rand()%100>20)
            {
                std::cout<<"0 ";
                continue;
            }
            else
            {
                std::cout<<"1 ";
                num++;
            }
        }
    
        std::cout<<"\nOccurrance: "<<num<<'%'<<std::endl;
        std::cin.get();
        return 0;
    }
    and some output:
    Code:
    0 0 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 1 0
    0 0 0 1 0 1 0 1 0 0
    0 0 0 0 1 0 0 0 1 0
    0 0 1 0 1 0 1 0 0 0
    0 1 0 0 0 0 1 0 0 0
    0 0 1 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 1 0 1 1 0
    Occurrance: 20%
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    That's a really nice example program major.

    I think the word you are looking for misplaced, is probability. I believe what you want is to call with a probability of 20% or 1 out of 5.

    This is a variation of the popular interview question on reading a random line from a file in one pass (in other words, you can't just read all the lines into memory and pick one from there).

    The technique you describe can be simply stated in one line of code, as long as you don't really need a random number between 0 and 100, which I don't think you do.

    See whether this works for you:
    Code:
      if ( rand() <= RAND_MAX / 5 ) {
          /* ... call ... */
      } else {
          /* ... raise ... */
      }
    What's the probability of rand() returning a number <= RAND_MAX / 5?

    HTH,
    Will

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's the probability of rand() returning a number <= RAND_MAX / 5?
    Not very uniform for values of RAND_MAX+1 not congruent to 5.

    Besides, I dont think it works.
    Let's pick a RAND_MAX for which RAND_MAX+1 is indeed congruent to 5.
    So suppose RAND_MAX=9

    Now, the range of values in the expression RAND_MAX/5 is [0, 1]
    Sure, it is evenly 0 or 1, but that's about it.
    The probability is 1/2=0.5, not 0.2 as required.

    To make it work, one might try
    rand() % 5 == 0, keeping RAND_MAX=9
    Now the range of values in the expression (rand() % 5) is [0, 4]
    This means that the probability is 2/5=0.2, as required.
    But since the value of RAND_MAX+1 is probably 2 with some positive exponent, it wont be congruent to 5, and so the range would not be uniform.

    EDIT:
    hmm... my analysis is incorrect (forgot we're simply using the whole range to compare with a subset, so the probability is correct), though I suggested a viable alternative, and the non-uniform problem remains.
    Last edited by laserlight; 03-24-2005 at 07:52 PM.
    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

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    hmm... my analysis is incorrect (forgot we're simply using the whole range to compare with a subset, so the probability is correct), though I suggested a viable alternative, and the non-uniform problem remains.
    Thank you for being so forthcoming.

    There's two differences that I would like to point out, although I'm pretty sure they are unimportant in this particular case.

    First:
    Code:
      rand() % 5
    is a run-time computation.

    Code:
      RAND_MAX / 5
    is a compile-time computation.

    Second:
    http://www.eskimo.com/~scs/C-faq/q13.16.html compares these two approaches from an implementation perspective. I think you will find it interesting.


    As far as uniformity is concerned, I'm not convinced that one method is better or worse. Additionally, I'm not sure that it would even be noticed, and I imagine the fix would outweigh the benefit. In other words, I think you're making a tempest in a teapot, and I would like some further explanation if you would be so kind.

    Will

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    compares these two approaches from an implementation perspective. I think you will find it interesting.
    That analysis doesnt apply for the same reason why my analysis isnt correct - we're not specifically selecting random numbers, we're using them for probability.

    As far as uniformity is concerned, I'm not convinced that one method is better or worse.
    They are the same.
    To correct that (minor) problem, one can restrict the selection of pseudo-random numbers to the largest range within RAND_MAX congruent to the range required.

    In other words, I think you're making a tempest in a teapot, and I would like some further explanation if you would be so kind.
    Actually, that's because I originally thought that your method was wrong - but that's a result of insufficient sleep after Lenten Vigil
    Sometime I posted it occurred to me that you're not talking about selecting random numbers, but about using them for probability.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM