• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Random or Math.random()???  RSS feed

 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Why this doesn't work?! My returned number is every time the same!!

and... what about Math.random(), how can i use, is it better than class Random for this purpose( generate a random number )?
Thanks!!
Sorry about my english!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the java api.
"If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers"
since you are seeding it with 49, you will always get the same values back. and getting the same values over and over is very helpful when you want to debug stuff.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, didn't finish.
you can use Math.random() to get a float between 0 and 1. then, a little arithmatic to get a value in the range you want. i'm sure there are some examples around if you search for them. generally, you'd multiply your float by the upper limit of what you want.
i.e. to get a value from 0 to 5 (not including 5)
float myFloat = Math.random() * 5;
if you want an integer, you can cast it...
int myInt = (int)Math.random() * 5; // gives integer value 0-4
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you are seeding it with 49
No, from the code shown, 49 is not the seed; it's the range of values that might be returned. The call to random.nextInt(49) should return an int value from 0 to 48.
I have a hard time imagining that the code shown will always return the same value, but I can definitely imagine that if you call it several times in succession you'll get the same value. That's because new Random() uses System.currentTimeMilis() as its seed, and this function is typically rounded to the nearest 10 ms. So if you call new Random() within 10 ms of the last time you called it, you probaby get the same seed as before, and thus the same number.
To avoid this, there are two strategies. One is to use a single Random stored in a field somewhere, and keep reusing that rather than creatng a new one for each random number:

Random is designed so that successive calls on the same instance will generate well-randomized values. It's just when you keep reseeding with the same value that you have a problem.
The other strategy is:
Here Math.random() has already taken care of the reseeding problem for you, behind the scenes. This method is a tiny bit slower, but simpler. Also the first method may not be entirely thread-safe if the same Random is accessed by multiple threads; I'm not sure about that part.
 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!!
Thank you buddies!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, i'm willing to admit that i may be wrong, but from Sun's api pages...
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html

Random
public Random(long seed)Creates a new random number generator using a single long seed:
public Random(long seed) { setSeed(seed); }Used by method next to hold the state of the pseudorandom number generator.
Parameters:
seed - the initial seed.

so, how is this not the seed value???
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind... if i actually READ the api, and READ the initial post, i'd figure it out...
mea culpa
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!