Advertisement
magnificentophat

Monte Carlo

Mar 8th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. public class MonteCarloHitDice {
  2.  
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5.  
  6. int[] hpTotal = new int[14];
  7. int[] avgHpTotal = new int[14];
  8.  
  9. for (int h=0; h<14; h++){
  10. hpTotal[h] = 0;
  11. avgHpTotal[h] = 0;
  12. }
  13.  
  14. for (int i=0; i<1000; i++){
  15. //clean out the hpTotal array
  16. for (int ii=0; ii<14; ii++){
  17. hpTotal[ii] = 0;
  18. }
  19.  
  20. //roll hp for first level
  21. hpTotal[0] = (int) ((Math.random()*8)+1);
  22.  
  23. //fill out the hp table for levels 1 through 14
  24. for (int j=1; j<14; j++){
  25. int tempTotal = 0;
  26. //are we at maximum hit dice?
  27. if (j<9){
  28. //roll jd6 and store it
  29. for (int k=0; k<j; k++){
  30. tempTotal += (int) ((Math.random()*8)+1);
  31. }
  32. }
  33. else{//okay, we're at maximum hit dice
  34. for (int k=0; k<9; k++){
  35. tempTotal += (int) ((Math.random()*8)+1);
  36. }
  37. tempTotal += 3*(j-8);
  38. }
  39. //is what we rolled bigger than the last level?
  40. if (tempTotal > hpTotal[j-1]){
  41. hpTotal[j] = tempTotal;
  42. }
  43. else{
  44. hpTotal[j] = hpTotal[j-1]+1;
  45. }
  46. }
  47. //add the hp values to the avHpTotal
  48. for (int l=0; l<14; l++){
  49. avgHpTotal[l] += hpTotal[l];
  50. }
  51. }
  52.  
  53. for (int m=0; m<14; m++){
  54. avgHpTotal[m] = avgHpTotal[m]/1000;
  55. }
  56.  
  57. for (int n=0; n<14; n++){
  58. System.out.println(avgHpTotal[n]);
  59. }
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement