• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Anyone good at Java?

Shanteli

Senior member

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?
 
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).
 
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.
 
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.
 
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.
 
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.
 
is speed important? If not then just put in a delay in your for loop so you won't get the same seed, duh!
 
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 🙁
 
Back
Top