Christopher
|
 |
«
Posted
2012-02-15 11:43:29 » |
|
hi all, Im using the following to generate the damage for weapons . 1 2
| Random r = new Random(); int damage = r.nextInt(6); |
I need to print the min and max to my stats page so it says something like, Damage: 1-6 Any ideas on how to implement this?
|
|
|
|
UprightPath
|
 |
«
Reply #1 - Posted
2012-02-15 11:50:08 » |
|
Well, first off. What you just showed, wound be a 0 indexed number. damage = [0,5] instead of [1,6]. nextInt returns a value from 0 - (value - 1). However, for what you need you can do something like this. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Weapon { weaponMin = 0; weaponRandom = 6;
public int rollDamage() { return weaponMin + random.nextInt(weaponRandom) + 1; }
public int getMin() { return weaponMin + 1; }
public int getMax() { return weaponMin + weaponRandom; } } |
|
|
|
|
ReBirth
|
 |
«
Reply #2 - Posted
2012-02-15 12:24:18 » |
|
My way 1
| offset + (int)(Math.random() * (max-min+1)) |
for 1-5, it'll be 1 + (int)(Math.random() * 5)
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Christopher
|
 |
«
Reply #3 - Posted
2012-02-15 12:29:49 » |
|
Well, first off. What you just showed, wound be a 0 indexed number.
I have been at it for double figures in hours... this just shows how much i need to call it quits for today.  Thank you for the reply, I had a feeling I may have to go that path, I was checking that their was nothing similar to .ceil in the Random methods that i didnt know of.
|
|
|
|
Roquen
|
 |
«
Reply #4 - Posted
2012-02-15 12:36:07 » |
|
[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]
|
|
|
|
|
ReBirth
|
 |
«
Reply #5 - Posted
2012-02-15 12:40:36 » |
|
@Roquen I care then 
|
|
|
|
Christopher
|
 |
«
Reply #6 - Posted
2012-02-15 13:49:28 » |
|
[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?
|
|
|
|
Roquen
|
 |
«
Reply #7 - Posted
2012-02-15 14:30:45 » |
|
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." 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).
|
|
|
|
|
Christopher
|
 |
«
Reply #8 - Posted
2012-02-15 14:39:50 » |
|
Thank you sir.
|
|
|
|
philfrei
|
 |
«
Reply #9 - Posted
2012-02-15 21:45:07 » |
|
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...
I recently hooked up the source code for Java and am learning how to look these things up. (Now just have to keep working on my comprehension.  ) 1 2 3
| public float nextFloat() { return next(24) / ((float)(1 << 24)); } |
|
"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
|
|
|
Games published by our own members! Check 'em out!
|
|
Riven
|
 |
«
Reply #10 - Posted
2012-02-15 21:59:11 » |
|
1 2 3
| public float nextFloat() { return next(24) / ((float)(1 << 24)); } |
Are you sure 24 distinct random values are enough for you? 
|
|
|
|
ra4king
|
 |
«
Reply #11 - Posted
2012-02-15 22:03:26 » |
|
My way 1
| offset + (int)(Math.random() * (max-min+1)) |
for 1-5, it'll be 1 + (int)(Math.random() * 5) 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 
|
|
|
|
Riven
|
 |
«
Reply #12 - Posted
2012-02-15 22:12:25 » |
|
Wrong, it's 1 + (int)(Math.random() * 6)
Or you can read what he said: for 1-5, it'll be 1 + (int)(Math.random() * 5) 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.
|
|
|
|
|
|
Matthias
|
 |
«
Reply #14 - Posted
2012-02-15 23:23:21 » |
|
1 2 3
| public float nextFloat() { return next(24) / ((float)(1 << 24)); } |
Are you sure 24 distinct random values are enough for you?  Random.next() is an internal method which takes the number of bits as argument.
|
|
|
|
|
Riven
|
 |
«
Reply #15 - Posted
2012-02-15 23:27:55 » |
|
Oops.  Thanks for the headsup.
|
|
|
|
Christopher
|
 |
«
Reply #16 - Posted
2012-02-16 01:24:18 » |
|
Now that i have had some sleep and my brain works again I have decided to go with the following as it removes having to write the +1's everywhere. Thanks for helping me get here guys. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Weapon { weaponMin = 1; weaponRandom = 6;
public int rollDamage() { return weaponMin + random.nextInt(weaponRandom); }
public int getMin() { return weaponMin; }
public int getMax() { return weaponMin + weaponRandom; } } |
|
|
|
|
StumpyStrust
|
 |
«
Reply #17 - Posted
2012-02-16 03:00:43 » |
|
Uhh...this is what I have been using and works great for me. 1 2 3 4 5 6 7 8 9 10 11 12 13
| public float random( float num ) { return ((num * 2) * rnd.nextFloat() - num); } public float randomPlus( float num ) { float temp = (float) ((num * 2) * rnd.nextFloat() - num); if( temp < 0 ) return temp * -1; else return temp; } |
But I use it to get random textures from an array where I want a chance to get 0....I have a feeling I could improve it based on what everyone here is saying.
|
|
|
|
|
ReBirth
|
 |
«
Reply #18 - Posted
2012-02-16 03:47:07 » |
|
Wrong, it's 1 + (int)(Math.random() * 6)
Or you can read what he said: for 1-5, it'll be 1 + (int)(Math.random() * 5) He gives code for a specific context ('for 1-5') and provides the answer. Yeeeee 
|
|
|
|
|