Anyone good at Java?

Shanteli

Senior member
Aug 7, 2000
568
0
0

I'm running a method that auto-plays a text recursive tree game. The output is supposed to be different because I have two random things going on:

Random ran = new Random();
int test = ran.nextInt(counter);

If I manually run the program it will give different outputs each time I run it, but if I have a FOR loop running it 5 times in my Main method, it gives the same output 3 times and then different output 2 times.

I am assuming the java.random crap is using the same CPU time "second" in
order to calculate the randomness.

Can anyone verify this or is there another way to have it calculate via miliseconds or something?

Or is there a way to "pause" a program for like a few seconds?
 

damonpip

Senior member
Mar 11, 2003
635
0
0
Did you put both lines inside the FOR loop? If so, you should only have the 2nd line inside the loop.
 

Shanteli

Senior member
Aug 7, 2000
568
0
0
Originally posted by: Argo
Link

Already looked...it didn't help....actually it NEVER helped.

Originally posted by: damonpip
Did you put both lines inside the FOR loop? If so, you should only have the 2nd line inside the loop.

No, the code I posted was just an example of the random part of it...not the actual method that is executing (One method is actually executing 3 other methods).
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: Shanteli
Originally posted by: Argo
Link

Already looked...it didn't help....actually it NEVER helped.

Look harder, smart guy.
1) This is off-topic, post in the software forum
2) Right on the api site for the Random class, it tells you that the no argument constructor looks like this:
public Random() { this(System.currentTimeMillis()); }

Since currentTimeMillis() is only really accurate down to the 10ms level on Windows (if you were running this on Linux you would have accuracy down to 1ms), yes, the second and third times through your loop you would get the same random seed.

Wow, I spent 15 seconds looking this stuff up. Re-think your additude and reask your question in the right location.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Why not try to manipulate it to use Math.random()?

If you're looking for an integer, simply do Math.random() * 1+whatever the largest value you want it ot have. Since its an int it will automatically be truncated to a whole number. Well you may have to cast, but thats the basic way you could do it.
 

Shanteli

Senior member
Aug 7, 2000
568
0
0
Originally posted by: Kilrsat
Originally posted by: Shanteli
Originally posted by: Argo
Link

Already looked...it didn't help....actually it NEVER helped.

Look harder, smart guy.
1) This is off-topic, post in the software forum
2) Right on the api site for the Random class, it tells you that the no argument constructor looks like this:
public Random() { this(System.currentTimeMillis()); }

Since currentTimeMillis() is only really accurate down to the 10ms level on Windows (if you were running this on Linux you would have accuracy down to 1ms), yes, the second and third times through your loop you would get the same random seed.

Wow, I spent 15 seconds looking this stuff up. Re-think your additude and reask your question in the right location.

For starters i posted here because I am lazy and needed a quick answer...so thanks you provided me with an explanation. And sorry, I'm not a "real" programmer so I have a hard time deciphering some of the terms and syntax on the API. And lastly....I'll rethink my "attitude" yeah...spelling buddy...while you rethink your tone.
 

Shanteli

Senior member
Aug 7, 2000
568
0
0
Originally posted by: BigJ
Why not try to manipulate it to use Math.random()?

If you're looking for an integer, simply do Math.random() * 1+whatever the largest value you want it ot have. Since its an int it will automatically be truncated to a whole number. Well you may have to cast, but thats the basic way you could do it.

Yup I am currently toying with Math.random to figure something out. Thanks for the suggestion.
 

shuan24

Platinum Member
Jul 17, 2003
2,558
0
0
is speed important? If not then just put in a delay in your for loop so you won't get the same seed, duh!
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: shuan24
is speed important? If not then just put in a delay in your for loop so you won't get the same seed, duh!

People who use sleep() to fix a problem should never be allowed to program again :(