Michael MacDonald
Michael MacDonald

Reputation: 433

What are some ways to store and recover numbers in this situation?

So i'm going to be running a simulator that plays craps.

My assignment requires me to run the sim 10,000,000 times.

None of that is an issue; I have the sim made and I know how to run in and I know how to create the required variables.

What I'm unsure of, is how I should go about storing the results of each game?

What I need to find in the end is:

Average # of Rolls Per Game Max # of Rolls in a game number of games that needed more than 30 rolls number of wins number of losses probability of a win longest sequence of wins and longest sequence of losses

All easy enough, I'm just not sure how to store 10,000,000 numbers and then access them easily.

For example the first: Average number of rolls should I create an arraylist that has 10,000,000 items in it? add one item at the end of each game and then add them all up and divide by 10,000,000?

I realize this should work, I'm just wondering if there is another way, or perhaps a better (more efficient) way.

New part to this question: Can I return more than one value from a method? Currently the simulation runs 10,000,000 times and returns a win or loss from each time. But I also need it to return the number of rolls from each game... Otherwise I can't figure out the values for avg rolls and highest number of rolls and number of games over 30 rolls.

Any ideas here?

Upvotes: 0

Views: 135

Answers (3)

Kshitij
Kshitij

Reputation: 361

You don't need to maintain array for any of the statistics you want.

For average number of rolls per game, just keep a variable, say cumulativeNumberOfRolls; after every game, just output the number of rolls in that game and add it to this variable. When all simulations are done, just divide this value by total number of simulations (10,000,000).

For max. number of rolls, again keep a single variable, say maxRolls; after every game, output the number of rolls in that game and compare that with this variable. If the number of rolls in this game is greater, then just update maxRolls with the new value. Try the same approach - of having a single variable and updating it after every game - to get the value for games that required more than 30 rolls, number of wins and number of losses. If you face problems, we can discuss them in comments.

For longest sequence of wins and losses, you would need to maintain a bunch of variables:

  • longest win sequence overall
  • longest loss sequence overall
  • current sequence count
  • current sequence type (indicates if current sequence is a win sequence or loss sequence)

Here's the overview of the approach. After every game, compare the result of the game with the current sequence type. If they are same, for instance result of current game is win and the current sequence type is also a win, then just update the current sequence count and move on to the next game. If they are different, you need to consider two scenarios and do slightly different things for them. I'll explain for one - the result of current game is loss and the current sequence type is win. In this scenario, compare current sequence count with longest win sequence overall and if it (current sequence count) is greater then just update the longest win sequence overall. After this, change the current sequence type to loss and set the current sequence count to 1.

Extend the above approach for the second scenario - the result of the current game is win and the current sequence type is loss. If you have clarifications, feel free to post back in comments.

Upvotes: 1

astidham2003
astidham2003

Reputation: 964

You could just calculate the statistics as you go without storing them. For instance, if you hava an "average" field in your class, then after each simulation average = ((number of rolls this game) + (total rolls so far)) / (number of games so far). The same could be done for the other statistics.

Upvotes: 1

Henry Keiter
Henry Keiter

Reputation: 17188

Well, you've got a fixed number of runs, so you might as well use an array rather than an arraylist (faster). It seems to me that you actually only need two total arrays: one listing the outcome of each game (maybe true/false for win/lose), and one with the number of rolls in that game. You fill these up as you run the simulations; then you get to do a bunch of simple math involving one array or the other to get your stats. That seems like the best way to go about it to me; I don't think you're going to get much more efficient without a lot of undue effort.

Upvotes: 0

Related Questions