Question
Problem Description: Game: Bean Machine or Galton Box. To figure out if the ball falls to Left or Right, you can generate a random number
Problem Description: Game: Bean Machine or Galton Box.
To figure out if the ball falls to Left or Right, you can generate a random number using Math.random(). If this random number is greater than or equal to 0.5 we assume the ball falls to the Right otherwise it falls to the Left (or vise versa).
If there are 8 slots, the ball should hit 7 nails (8 -1), thus you should run a loop 7 times to figure out the path for that ball. If you have N balls, this whole process should be repeated N times. Here is a basic algorithm.
# of balls = N
# of slots = K
array of K elements
for ( i = 0 to N) {
int R = 0;
for (j = 0 to K-1) {
if ( random number >= 0.5)
R++;
}
array[R] ++;
}
Analysis: Use as much space as needed.
(Describe the problem including input and output in your own words.)
Design: Use as much space as needed.
(Describe the major steps in your algorithm for solving the problem. Or, draw a flow chart, if you can.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started