Need help setting up arrays for Galton box simulation?

  • Java
  • Thread starter irresistible
  • Start date
  • Tags
    Arrays Java
In summary, the conversation is about creating a program to simulate the bean machine or Galton box, where balls are dropped from the top and have equal chances of falling to the left or right at each row. The goal is to set up the algorithm and arrays needed to track the ball's movement and accumulation in the slots at the bottom of the board. One suggested method is to use a Pascal's triangle format to store the data, with each slot having equal chances of falling into the two slots below it. The number of slots required can be calculated using the formula n(n+1)/2, where n is the number of rows.
  • #1
irresistible
15
0
I'm new in java and
I'm trying to come up with a program that stimulates the bean machine(also known as Galton box)
Balls are dropped from the opening of the board. Everytime a ball hits, there is a 50% chance to fall to left and 50% chance to fall to the right. the piles of balls are accumulated in the slots at the bottom of the board.

I'm just trying to set up my algorithm, but I don't know how to set up my arrays. i.e, the entreis in the arrays, the Givens, results, etc.

This is a Galton box and how it works in case you don't know:
http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Galton_Box.svg/507px-Galton_Box.svg.png

I just don't know where to start.. how to set up my arrays.
Any help is appreciated.:smile:
 
Technology news on Phys.org
  • #2
You can store the data in the form of a Pascal's triangle.
The top slot will be numbered 1.
The second row will have slots 2 and 3, third row 4,5,6, and so on.

Code:
       1
      2 3
     4 5 6
    7 8 9 10
This way, the marble/ball falls in slot one will have equal chances of falling into slots 2 or 3.
From 2 (row 2) there will be equal chances of falling into 4 or 5, and from 5 (row 3) it will have equal chances for falling into slots 8 or 9, and so on.
In short, from slot m on row n, the two possible destination slots are m+n and m+n+1.
You will only need to set up an array to store all the slots, start with slot one, keep track of the row number and keep the ball falling! The number of slots required is n(n+1)/2, where n is the number of rows.
 
  • #3


Hello,

Setting up arrays for a Galton box simulation can be a bit tricky, but with some guidance, you will be able to do it successfully. The first thing you need to do is determine the size of your arrays. This will depend on the number of rows and columns you want in your Galton box. For example, if you want a 5x5 Galton box, you will need to create two arrays with a size of 5 (one for the rows and one for the columns).

Next, you will need to decide what data type your arrays will hold. In this case, you will need to store the number of balls in each slot at the bottom of the box. So, you can use an integer data type for your arrays.

Once you have created your arrays, you will need to initialize them with the starting values. This will depend on the specific requirements of your simulation. For example, if you want to start with 10 balls in the top slot, you will set the first element of your array to 10.

As for the algorithm, you can use a loop to simulate the balls falling through the box. Inside the loop, you can use a random number generator to determine whether the ball will fall to the left or right. Based on this result, you can update the values in your arrays accordingly.

I hope this helps you get started with setting up your arrays for your Galton box simulation. If you need further assistance, don't hesitate to reach out for help. Good luck with your program!
 

1. What is an array in Java?

An array in Java is a data structure that allows you to store a collection of elements of the same data type. It is a fixed-size container that can hold multiple values, making it easier to organize and manipulate data.

2. How do I declare an array in Java?

To declare an array in Java, you need to specify the data type of the elements it will hold, followed by square brackets [] and the array name. For example, "int[] myArray;" will declare an array of integers named "myArray".

3. How do I initialize an array in Java?

To initialize an array in Java, you can use the "new" keyword followed by the data type and the size of the array in square brackets. For example, "int[] myArray = new int[5];" will initialize an array of 5 integers named "myArray".

4. How do I access elements in an array in Java?

You can access elements in an array in Java using their index number. The index starts at 0 for the first element, and you can use square brackets [] after the array name to specify the index. For example, "myArray[2]" will access the third element in the array "myArray".

5. How do I add or remove elements from an array in Java?

Arrays in Java have a fixed size, so you cannot add or remove elements from them. However, you can create a new array with a different size and copy the elements from the original array to the new one. Alternatively, you can use other data structures like ArrayList, which allows for dynamic resizing and adding/removing elements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
9
Views
2K
Replies
11
Views
3K
  • Art, Music, History, and Linguistics
Replies
1
Views
999
Replies
1
Views
1K
Replies
4
Views
1K
Back
Top