2 ms·
Er, actually it looks as if exactly what this does depends on what version you're using. The Java Platform SE 6 docs say "This constructor sets the seed of the
by gjm11 17y ago
Er, actually it looks as if exactly what this does depends on what version you're using. The Java Platform SE 6 docs say "This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor." Same for 5. But 1.4.2 has the text I quoted above.
In any case, creating a new Random object every time you want a random number is unlikely to give satisfactory results. There is certainly no guarantee that the implementation will try to make it do so.
- tspiteri 17y agoStill, if the point of using the Random object is being efficient, instantiating an object for just one nextBoolean() call is likely to be more inefficient than just calling Math.random().
- city41 17y agoIt would depend on how Math.random() is implemented. It could be "return new Random().nextDouble();" I say that because if memory serves, Integer.parseInt(string s) is just "return new Integer(s).intValue();"
- chubbard 17y agoReread the docs! The API hasn't changed from 1.4 to 1.6. nextInt(x)'s is used to generate numbers from [0,x) Maybe in 1.6 they started using a better seeding source for randomness since they added System.nanoTime(). But, your original objection is a wrong interpretation of the API docs.
- Tichy 17y agoLet's apply some logic: the seed can not be very random, otherwise one could just use the seed generator instead of the random generator.
- gjm11 17y agoHuh? I didn't say anything about nextInt. The point is that if every time you want a random number you construct a new Random object using the default constructor, then (1) if you're using version 1.4 or earlier then rapidly repeated "random" number generation will give you the same result over and over, and (2) if you're using version 1.5 or later then it's (quite rightly) almost entirely unspecified what you'll get and whether it will be "random enough" for any particular purpose and whether it will be terribly inefficient. (I'm wondering whether perhaps you missed the fact that lucifer's proposed constructions involve making a new Random object each time you want some randomness.)