How do I change the probabilities of a number generator in java?
Printable View
How do I change the probabilities of a number generator in java?
I don't understand the question; probabilities don't really play directly into random generation. If you mean guaranteeing a consistent set, you can use a seed for your random numbers. The default seed in Java is (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)
, but you can change that to 1 if you want to. That doesn't mean you end up with 1, 2, 3, . . ., but it does mean that if you run two randoms with the same seed it will result in the same numbers.
Here is the setup of the problem that I need to solve.
I just need help with the probabilities part though.Quote:
In his book, Full House, Stephen Jay Gould uses illustrations to help make a point about the progression (or lack or progression) of a species toward greater complexity. Such examples include the disappearance of the .400 batting average in baseball and a drunk randomly staggering along a wide side walk bounded on one side by a building wall and on the other by a curb. Of course, once against the wall, the drunk can stagger no closer; however, he is free to stagger in the opposite direction until he falls off the curb into the street. In the middle (or interior) of the sidewalk, he can stagger in either direction. We will go no further in explaining how these relate to the progression of complexity in a species.
The example of the drunk staggering along the sidewalk can be extended to a two-dimensional situation to obtain some interesting paths of staggering in the following way. Imagine a -shaped building enclosing a rectangular court yard, bounded on three sides by building walls and open on the remaining side. The plot can be simulated by an M X N two-dimensional array. We will let our drunkard start in the upper left hand corner, [0][0] position, and then randomly move in horizontal, vertical or diagonal directions one step at a time. Diagonal progressions are allowed. Of course, if he is next to the wall he can’t move any farther in the direction toward the wall. For example, in the upper left corner, if his direction is determined to be to the left and up, he can’t move, and that would count him as visiting that same position again. Now we let him walk randomly, counting the number of times he hits each square and counting each step, even if he stays in the same place. He walks until he exits downward from the bottom row.
In this assignment you are to write a class to represent the courtyard bounded on three sides by three buildings forming a rectangular courtyard. With this class you will need to instantiate an object representing :
the courtyard,
the location of the drunk inside the yard both row and column,
the number of steps he has taken,
whether or not he has stepped out of the courtyard through the non-bounded edge,
how many times he has visited each square in the array,
two one-dimensional arrays of doubles each of length 3 holding the probabilities that his horizontal move is -1(left), 0, 1(right) and the probabilities that his vertical move is -1(up), 0, 1(down).
When making a one-step move here are the rules. Based on the probabilities given, first decide which direction to move to a new row, by computing -1, 0, or 1. If this move is 1 and it would take him out of the courtyard, set the “outofbounds” flag to true, and don’t do anything else (other than increment the number of steps). If it is -1 and he is already against the top boundary, don’t change rows. After the row is determined, compute a column move, keeping in mind that if there is an attempt to go beyond either wall (left or right) the column does not change. When the row and column locations have been updated, then increment the corresponding courtyard location and also increment the number of steps. These are incremented even if he does not move.
Non-static Methods Needed
A constructor to initialize the courtyard to the dimensions specified as arguments. It should also appropriately initialize the array and all instance variables in the class. The two probability vectors should each be set to .33333, .33333, .33333 (representing equal likelihood to move in any of the directions.
A method to advance the drunk one time unit based on the rules for moving described above.
A method to set new probabilities for a vertical move.
A method to set new probabilities for a horizontal move.
A method to return true if the drunk has stepped out of the courtyard and false otherwise.
A method to print the drunk’s history after he has exited the courtyard. This method will print M rows of characters with N characters on each line, where M and N represent the dimensions of the courtyard. The character printed will be based on the integer value of each entry in the array, based on the following mapping:
o 0: print a blank
o 1 to 4: print ‘-‘
o 5 to 9: print ‘0’
o 10 to 14: print ‘*’
o > 14: print ‘@’
After printing these characters, the last thing it should do is print vertically E X I T and then print a line indicating that he stepped out in column ? after ? steps. See sample output below.
Static Variables and Methods
There should be one static variable of class type Random to be used in computing the direction the drunk moves.
There should be one static method with a double array parameter, that will return -1, 0, or 1 based on the three probabilities in the array.
The main program should do the following:
Ask the user to input the number of rows and columns;
Instantiate an object with those rows and columns;
Ask the user if he or she desires to change the probabilities;
o if no, then proceed with running the program with equal probabilities.
o if yes, then get new probabilities for vertical and horizontal moves. Then reset the probabilities using appropriate methods. NOTE: Probabilities should be between 0 and 1 inclusive, and the sum of the three should be 1.0. However, you don’t want to compare doubles for equality, so test that Math.abs(sum-1.0) <= 1.0E-5.
Let the program run until the drunk exits the courtyard.
Print the configuration of the courtyard based on the character mappings given above
Oh I see what you mean. Yes that's very easy; you simply draw a number and see if its <= your probability. So with probability's that are set as a float, you can use Math.random and simply see if your given probability is <= the number selected. Math.random() will return a number between 0 and 1.
post your code and lets check it out.