Programming question

Discussion in 'C++' started by Pat, Aug 24, 2004.

  1. Pat

    Pat Guest

    I am a C++ beginner, please give me some suggestion on the following
    question.

    Given n balls. The probability pi is the chance to choose ball i. Sum of pi
    is 1.

    I want to run 10000 independent trials in selecting the ball, and simulate
    the expected number of each ball to be choosen.

    I have no idea how to implement the probability drawing step. Could you give
    me some hints?
    Thanks.

    Pat
     
    Pat, Aug 24, 2004
    #1
    1. Advertising

  2. Pat

    Daniel T. Guest

    In article <412b7e92$-cable.com>, "Pat" <> wrote:

    > I am a C++ beginner, please give me some suggestion on the following
    > question.
    >
    > Given n balls. The probability pi is the chance to choose ball i. Sum of pi
    > is 1.
    >
    > I want to run 10000 independent trials in selecting the ball, and simulate
    > the expected number of each ball to be choosen.
    >
    > I have no idea how to implement the probability drawing step. Could you give
    > me some hints?


    Look up how to use the 'rand()' function.
     
    Daniel T., Aug 24, 2004
    #2
    1. Advertising

  3. Pat wrote:
    > I am a C++ beginner, please give me some suggestion on the following
    > question.
    >
    > Given n balls. The probability pi is the chance to choose ball i. Sum of pi
    > is 1.
    >
    > I want to run 10000 independent trials in selecting the ball, and simulate
    > the expected number of each ball to be choosen.
    >
    > I have no idea how to implement the probability drawing step. Could you give
    > me some hints?


    This is not really a C++ language question, and as such it doesn't belong
    here, in all honesty. Please ask generic programming questions in
    comp.programming and generic mathematics questions in sci.math.

    Some hints: usually simulating with a computer something that occurs at
    random requires the use of pseudo-random number generator. There is one
    in the Standard C++ library. Its interface consists of two functions
    named 'rand' and 'srand'. Since computers are pretty much deterministic
    devices when it comes to programmed behaviour, simulating real-time random
    situations with computers is tricky and requires some assumptions to be
    made. You need to figure out what "10000 independent trials" really means
    because if it's all in the same program, it's not really _independent_.

    Victor
     
    Victor Bazarov, Aug 24, 2004
    #3
  4. It would probably be quicker to design the algorithm in something like
    MATLAB first (in this case 10 min) then look at implementing it in C++ once
    you know what your doing if you need to.

    One algorithm is:

    % Generate probabilities
    n=100; % Number of balls
    trials=100000; % Number of independant trials
    pi=rand(n,1); % n different probabilities of a ball being choosen
    pi=pi/sum(pi); % Ensure sum is 1
    cumpi=cumsum(pi); % Cumulative sum of probabilities

    %% Simulate
    count=zeros(n,1); % Set all ball counts to 0
    for i = 1:trials
    pick=rand; % Pick a number between 0 and 1
    choosen=sum(cumpi<pick)+1; % Find which ball this corresponds too
    count(choosen)=count(choosen)+1; % Count ball
    end

    Stuart

    "Pat" <> wrote in message news:412b7e92$-cable.com...
    > I am a C++ beginner, please give me some suggestion on the following
    > question.
    >
    > Given n balls. The probability pi is the chance to choose ball i. Sum of

    pi
    > is 1.
    >
    > I want to run 10000 independent trials in selecting the ball, and simulate
    > the expected number of each ball to be choosen.
    >
    > I have no idea how to implement the probability drawing step. Could you

    give
    > me some hints?
    > Thanks.
    >
    > Pat
    >
    >
    >
     
    Stuart McGarrity, Aug 24, 2004
    #4
  5. Pat

    Kai-Uwe Bux Guest

    Pat wrote:

    > I am a C++ beginner, please give me some suggestion on the following
    > question.
    >
    > Given n balls. The probability pi is the chance to choose ball i. Sum of
    > pi is 1.
    >
    > I want to run 10000 independent trials in selecting the ball, and simulate
    > the expected number of each ball to be choosen.
    >
    > I have no idea how to implement the probability drawing step. Could you
    > give me some hints?
    > Thanks.
    >
    > Pat


    a) This is off-topic here.
    b) Google for "J.A. Walker's alias method" or look up

    D.E. Knuth
    The Art of Computer Programming Vol 2,
    page 120--121

    for a description.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 24, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salisha Khan
    Replies:
    1
    Views:
    11,684
    JohnFol
    Aug 1, 2003
  2. Clemens Ragger

    programming question

    Clemens Ragger, Feb 3, 2005, in forum: VHDL
    Replies:
    3
    Views:
    462
    Tim Hubberstey
    Feb 4, 2005
  3. Matt
    Replies:
    35
    Views:
    10,707
    George Neuner
    Jul 22, 2004
  4. Casey Hawthorne
    Replies:
    4
    Views:
    1,037
    Jarek Zgoda
    Aug 4, 2006
  5. Joe Mayo
    Replies:
    168
    Views:
    3,424
    David Thompson
    Oct 22, 2007
Loading...

Share This Page