473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Programming question

Pat
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

Jul 22 '05 #1
4 1415
In article <41**********@rain.i-cable.com>, "Pat" <Pa*@Pat.com> 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.
Jul 22 '05 #2
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
Jul 22 '05 #3
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" <Pa*@Pat.com> wrote in message news:41**********@rain.i-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

Jul 22 '05 #4
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rdsteph | last post by:
I am having a lot of fun using the pyGoogle module ( http://pygoogle.sourceforge.net/ ) that uses the Google API. It is about as easy to use as I can imagine, and it is a lot nicer than using my...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
10
by: Rohit | last post by:
I need some ideas on how to write a program that could -- Read an MS Access database and grab information say Vehicle year, vehicle make -- Make a call to a website and enter the information...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
7
by: Jesse B. | last post by:
I've been learning how to program with C, and I can't find any info about GUI programming with C. I'm almost done with O'reilly's Practical programming with C, and would like to mess around with...
2
by: Ed_P | last post by:
Hello I just wanted to get the opinions of those of you who have experience developing C# applications and programming in general. I currently am learning the basics of programming (choosing C#...
9
by: tjones | last post by:
Hi, I am guessing this is *THE* nntp newsgroup for C#? I was hoping someone could point me in the right direction. Id like to find employment in the IT industry as a programmer again. My...
42
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming...
11
by: arnuld | last post by:
hello all, 1st of all, i searched last 12 years archives of comp.lang.c++ because i have some problems. i got some help but not satisfied as i did not get solution specific to my problem. In the...
23
by: Dexter | last post by:
My site is home to series of quizzes ranging from Accounting, Business, Math to programming languages. These are multiple choice type questions and you get a score card at end. For C language, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.