[NonNewbieMode]Note: Math.random calculates a integer, which is mapped into a float (type conversion + multiply). Then you multiply that and convert back into an integer. Another downside, if you're multi-threaded and multiple threads call Math.random, is that they will cause cache trashing if occurring roughly at the same time. Do you care? Probably not.[/NonNewbieMode]

[NonNewbieMode]Note: Math.random calculates a integer, which is mapped into a float (type conversion + multiply). Then you multiply that and convert back into an integer. Another downside, if you're multi-threaded and multiple threads call Math.random, is that they will cause cache trashing if occurring roughly at the same time. Do you care? Probably not.[/NonNewbieMode]

Not that this should be a problem in my single threaded, turn based RPG but what is the faster, multi-thread safe alternative?

Some clarifications are in order. First java.util.Random's nextFloat hopefully (too lazy to check) performs a divide instead of a multiply for conversion. (And java.math.Random() just calls a nextDouble on a static internal instance. nextDouble will need to create two integers, combine and divide). Second, what I meant by "Do you care? Probably not." is "Should you care? Probably not unless you're creating many random values in a short time window."

Quote

Not that this should be a problem in my single threaded, turn based RPG but what is the faster, multi-thread safe alternative?

Note that Random is thread safe. But the answer to your question is...it depends on what you're doing and your goals. If you're not creating tons of random numbers, this really isn't an issue to worry about. Personally what I do is use a lightweight RNG where the seed data is store with each entity. This allows me to run deterministically if I need to debug, prevents cache thrashing and increases locality of the current working data set. My guess is that most people would find that a PITA as you have to reseed each entity when loading (and not debugging).

Wrong, it's 1 + (int)(Math.random() * 6) since Math.random() returns a number between 0 and 1, including 0 and excluding 1. This means that it will never be 1, so your code will never produce a value of 6. Since there are 6 values that the OP needs (1-6 = 6 distinct numbers), then you want a number from 0 to 6, including 0 and excluding 6, and simply add 1 to it

He gives code for a specific context ('for 1-5') and provides the answer.

Taking things out of context and rambling about some implementation detail that the poster is clearly aware of, as his example takes that into account, is a sure way to decrease the signal to noise ratio.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org