3

When I set a new Random with a seed, the first time I call it I always get numbers in a very similar range. Subsequent calls seem to actually get me a random number.

    for(int i=1;i<100;i++){
        System.out.print(Integer.toString(i)+" ");
        Random r = new Random(i);
        System.out.print(r.nextDouble());
    }

1: 0.7308781907032909 2: 0.7311469360199058 3: 0.731057369148862 4: 0.7306094602878371 5: 0.730519863614471 6: 0.7307886238322471 7: 0.7306990420600421 8: 0.7302511331990172 9: 0.7301615514268123 10: 0.7304302967434272 11: 0.7303407149712222 12: 0.7298928061101974 13: 0.7298032243379924 14: 0.7300719696546073 15: 0.7299823878824023 16: 0.7324010808307754 17: 0.7323115139597316 18: 0.7325802592763465 19: 0.7324906626029803 20: 0.7320427537419555 21: 0.7319531868709117 22: 0.7322219172863654 23: 0.7321323355141605 24: 0.7316844415542968 25: 0.7315948597820918 26: 0.7318635901975455 27: 0.7317740084253406 28: 0.7313261144654769 29: 0.7312365177921107 30: 0.7315052631087257 31: 0.7314156962376819 32: 0.7281011855672591 33: 0.7280116037950541 34: 0.7282803491116691 35: 0.7281907524383029 36: 0.7277428584784392 37: 0.7276532767062343 38: 0.7279220220228492 39: 0.727832425349483 40: 0.7273845313896193 41: 0.7272949496174144 42: 0.7275636800328681 43: 0.7274741131618243 44: 0.7270262043007995 45: 0.7269366225285945 46: 0.7272053678452094 47: 0.7271157860730044 48: 0.7295344790213775 49: 0.7294448972491725 50: 0.7297136425657874 51: 0.7296240607935824 52: 0.7291761519325576 53: 0.7290865701603526 54: 0.7293553154769675 55: 0.7292657486059237 56: 0.7288178248437377 57: 0.7287282430715327 58: 0.7289970032893088 59: 0.7289074066159427 60: 0.7284594977549178 61: 0.728369930883874 62: 0.7286386762004889 63: 0.7285490795271228 64: 0.7252345688567 65: 0.725144987084495 66: 0.7254137473022712 67: 0.7253241655300662 68: 0.7248762417678801 69: 0.7247866748968363 70: 0.7250554202134513 71: 0.7249658384412463 72: 0.7245179146790602 73: 0.7244283478080165 74: 0.7246970931246314 75: 0.7246075113524264 76: 0.7241596024914015 77: 0.7240700207191966 78: 0.7243387660358115 79: 0.7242491693624453 80: 0.7266678772119796 81: 0.7265782954397746 82: 0.7268470407563895 83: 0.7267574589841845 84: 0.7263095650243209 85: 0.7262199683509547 86: 0.7264887136675696 87: 0.7263991318953646 88: 0.725951237935501 89: 0.7258616412621348 90: 0.7261303865787497 91: 0.7260408048065448 92: 0.7255929108466811 93: 0.7255033141733149 94: 0.7257720594899298 95: 0.7256824926188861 96: 0.7223679819484633 97: 0.7222784001762583 98: 0.722547130591712 99: 0.7224575488195071

    for(int i=1;i<100;i++){
        System.out.print(Integer.toString(i)+" ");
        Random r = new Random(i);
        r.nextDouble();
        System.out.print(r.nextDouble());
    }

1: 0.41008081149220166 2: 0.9014476240300544 3: 0.07099203475193139 4: 0.9187140138555101 5: 0.08825840967622589 6: 0.5796252073129174 7: 0.7491696031336331 8: 0.5968915822372118 9: 0.7664359929590888 10: 0.2578027905957804 11: 0.42734718641649616 12: 0.2750691655200749 13: 0.44461356134079055 14: 0.9359803887798046 15: 0.10552478460052028 16: 0.527826082540034 17: 0.6973704783607497 18: 0.18873727599744128 19: 0.35828168671931826 20: 0.20600366582289698 21: 0.37554806164361276 22: 0.8669148741814655 23: 0.03645927000218119 24: 0.88418124910576 25: 0.053725659827636885 26: 0.5450924574643284 27: 0.7146368532850442 28: 0.5623588323886229 29: 0.7319032282093387 30: 0.22327004074719148 31: 0.39281443656790715 32: 0.6659570968355509 33: 0.8355014926562667 34: 0.32686830519411947 35: 0.49641270101483514 36: 0.34413468011841397 37: 0.5136790759391296 38: 0.00504590337814359 39: 0.17459029919885938 40: 0.022312278302438093 41: 0.19185667412315388 42: 0.6832234717598454 43: 0.8527678824817223 44: 0.700489861585301 45: 0.8700342574060168 46: 0.36140105504270836 47: 0.5309454508634242 48: 0.953246763704099 49: 0.12279115952481479 50: 0.6141579720626675 51: 0.7837023678833833 52: 0.631424346986962 53: 0.8009687428076777 54: 0.29233554044436927 55: 0.46187995116624625 56: 0.30960193026982497 57: 0.47914632609054075 58: 0.9705131386283935 59: 0.1400575344491093 60: 0.987779513552688 61: 0.15732392427456487 62: 0.6486907219112564 63: 0.8182351177319722 64: 0.09137779290077719 65: 0.26092217382033167 66: 0.7522889863581844 67: 0.9218333821789002 68: 0.7695553612824789 69: 0.9390997720043559 70: 0.43046656964104746 71: 0.6000109654617632 72: 0.44773294456534196 73: 0.6172773403860576 74: 0.10864416782507158 75: 0.2781885487446262 76: 0.12591054274936608 77: 0.2954549236689207 78: 0.7868217511079346 79: 0.9563661469286503 80: 0.37866742996700287 81: 0.5482118406888798 82: 0.039578653226732596 83: 0.20912304904744827 84: 0.05684502815102699 85: 0.22638940907058158 86: 0.7177562365095955 87: 0.8873006323303113 88: 0.73502261143389 89: 0.9045670072546057 90: 0.39593381979245856 91: 0.5654782156131742 92: 0.41320019471675296 93: 0.5827445905374687 94: 0.07411140307532149 95: 0.24365579889603728 96: 0.516798459163681 97: 0.6863428549843967 98: 0.17770968242341068 99: 0.34725406334296527 FINAL SUMMARY

This gets me random numbers. Is this expected? Why? I'm running:

  • Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
  • Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Those are rather small seed values. Using something like `System.currentTimeMillis()` is more generally used, and more-or-less a unique seed value. – SnakeDoc Feb 20 '17 at 21:27
  • Thanks. I actually need the user to input the random seed and they will use small ints. I wasn't using the right keywords to find the duplicated question but that answers my question. – Josep Valls Feb 20 '17 at 21:38

1 Answers1

1

In your first code you print the FIRST random number, after seeding with very "close" values.

But in your second loop you are calling nextDouble twice, and you are only printing the results of of the second call!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Exactly, because the first one is giving me the 0.7 values. – Josep Valls Feb 20 '17 at 21:38
  • Probably I should have made the *close* seed part more explicit as that was meant as answer... Worth at least a upvote not only a "yes true" comment ;-) – GhostCat Feb 21 '17 at 05:14