Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Max and Min of Random  (Read 2346 times)
0 Members and 1 Guest are viewing this topic.
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« 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?

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

Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline Christopher

Senior Member


Medals: 2
Projects: 1



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

Offline 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]
Offline ReBirth
« Reply #5 - Posted 2012-02-15 12:40:36 »

@Roquen
I care then Grin

Offline Christopher

Senior Member


Medals: 2
Projects: 1



« 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?

Offline 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."

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

Senior Member


Medals: 2
Projects: 1



« Reply #8 - Posted 2012-02-15 14:39:50 »

Thank you sir.

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

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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« 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? Lips Sealed

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

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« 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 Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2012-02-15 22:12:25 »

Wrong, it's 1 + (int)(Math.random() * 6)

Or you can read what he said:
Quote
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.

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

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #13 - Posted 2012-02-15 22:22:31 »

Lips Sealed I need to look closer while reading things Grin

Offline Matthias

Senior Member


Medals: 3
Projects: 1


TWL - Themable Widget Library


« 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? Lips Sealed
Random.next() is an internal method which takes the number of bits as argument.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2012-02-15 23:27:55 »

Oops. Emo

Thanks for the headsup.

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

Senior Member


Medals: 2
Projects: 1



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


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

Offline 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:
Quote
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 Tongue

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!