Max and Min of Random

hi all,

Im using the following to generate the damage for weapons .



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?

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.


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;
}
}

My way

 offset + (int)(Math.random() * (max-min+1))

for 1-5, it’ll be 1 + (int)(Math.random() * 5)

I have been at it for double figures in hours… this just shows how much i need to call it quits for today. :emo:

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.

[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]

@Roquen
I care then ;D

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.”

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).

Thank you sir.

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. :slight_smile: )

    public float nextFloat() {
        return next(24) / ((float)(1 << 24));
    }

Are you sure 24 distinct random values are enough for you? :-X

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 :slight_smile:

Or you can read what he said:

[quote]for 1-5, it’ll be 1 + (int)(Math.random() * 5)
[/quote]
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.

:-X I need to look closer while reading things ;D

Random.next() is an internal method which takes the number of bits as argument.

Oops. :emo:

Thanks for the headsup.

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.


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;
}
}

Uhh…this is what I have been using and works great for me.


    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.

He gives code for a specific context (‘for 1-5’) and provides the answer.
[/quote]
Yeeeee :stuck_out_tongue: