Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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 2703 times)
0 Members and 1 Guest are viewing this topic.
Offline Christopher

Senior Devvie


Medals: 2
Projects: 1



« Posted 2012-02-15 10: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?

Offline UprightPath
« Reply #1 - Posted 2012-02-15 10: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 11: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 Devvie


Medals: 2
Projects: 1



« Reply #3 - Posted 2012-02-15 11: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 11: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 11:40:36 »

@Roquen
I care then Grin

Offline Christopher

Senior Devvie


Medals: 2
Projects: 1



« Reply #6 - Posted 2012-02-15 12: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 13: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 Devvie


Medals: 2
Projects: 1



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

Thank you sir.

Offline philfrei
« Reply #9 - Posted 2012-02-15 20: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));
    }

"It's after the end of the world! Don't you know that yet?"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-02-15 20: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: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-02-15 21: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: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2012-02-15 21: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: 355
Projects: 3
Exp: 5 years


I'm the King!


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

Lips Sealed I need to look closer while reading things Grin

Offline Matthias

Senior Devvie


Medals: 3
Projects: 1


TWL - Themable Widget Library


« Reply #14 - Posted 2012-02-15 22: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: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2012-02-15 22: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 Devvie


Medals: 2
Projects: 1



« Reply #16 - Posted 2012-02-16 00: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 02: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 02: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.

toopeicgaming1999 (55 views)
2014-11-26 15:22:04

toopeicgaming1999 (48 views)
2014-11-26 15:20:36

toopeicgaming1999 (8 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (26 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!