This question already has an answer here:
Not sure how to explain this, if 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());
}
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)
Aucun commentaire:
Enregistrer un commentaire