# Library Random Numbers

Discussion in 'Resources' started by Kakarot798, Oct 9, 2015.

Not open for further replies.
1. Offline

### Kakarot798

Hello, this is my first attempt at something like this, and will probably get better with this over time.

I was bored so I wrote out a class file that contains many methods for random numbers.
It contains methods to :
• Get random ranged Integers, Floats, Longs, and Doubles
• Get random non-ranged Integers, Floats, Longs, and Doubles
• Get random rounded Floats, Longs, and Doubles
I'll state once again, this is my first attempt at this type of thing, and am not sure how well I did at posting this as well as making it useful.

Just create a class file and name it "NewRandom", then post this code in it:
Code:
```public class NewRandom {

/**
* @version 1.0
* This Random Number Lib (Class) was coded by <Kakalavala>
* Please do not post it as yours.
*/

private static Random rand = new Random();

protected static int getRangedInt(int highest, int lowest){
int rn = rand.nextInt();
if(!(rn > highest || rn < lowest)) return rn;
else return getRangedInt(highest, lowest);

}

protected static double getRangedDouble(double highest, double lowest){
double rn = rand.nextDouble();
if(!(rn > highest || rn < lowest)) return rn;
else return getRangedDouble(highest, lowest);

}

protected static float getRangedFloat(float highest, float lowest){
float rn = rand.nextFloat();
if(!(rn > highest || rn < lowest)) return rn;
else return getRangedFloat(highest, lowest);
}

protected static long getRangedLong(long highest, long lowest){
long rn = rand.nextLong();
if(!(rn > highest || rn < lowest)) return rn;
else return getRangedLong(highest, lowest);
}

protected static int getInt(){
return rand.nextInt();
}

protected static double getDouble(){
return rand.nextDouble();
}

protected static float getFloat(){
return rand.nextFloat();
}

protected static long getLong(){
return rand.nextLong();
}

protected static int getRoundedDouble(){
return (int) Math.round(rand.nextDouble());
}

protected static int getRoundedFloat(){
return (int) Math.round(rand.nextFloat());
}

protected static int getRoundedLong(){
return (int) Math.round(rand.nextLong());
}
}

```
I can honestly bet that anyone will be able to make this better than I did, and I'd appreciate the help on making the code better, as well as this post sharing it!

I replaced the "new Random()" with a private static Random from the start of the code to reduce the memory issue! Hopefully I made it better!

Last edited: Oct 9, 2015
#1
2. Offline

### Forge_User_95269065

@Kakarot798
Why are you creating a new Random object each time you roll a random number?

3. Offline

### Kakarot798

It was the way I've always done it, but I'm guessing I can just do:
Code:
`private Random rand = new Random();`
And instead of new Random().blah I can just do rand.blah and not generate a new number?

#3
4. Offline

### Forge_User_95269065

@Kakarot798
You're wasting a lot of memory by creating new Random objects, I can't think of any reason to do this in a plugin. So yeah, just create one random object somewhere in the class and reuse it.

5. Offline

### teej107

Don't mean to be rude but this is terrible code and here is why
1. This is just a wrapper for the Random class. You can obtain numbers easily with just the Random class alone.
2. Each getRanged method could theoretically get called on an infinite loop and could cause StackOverflowException.
Obtaining a random number from the random class can be easily done like this:
Code:
`Random#nextInt(number)`
As stated in the JavaDocs for Random, the method Random#nextInt(int)
If you wanted a number between -500 and 300 (exclusive), you could just do this:
Code:
`int randomNumber = myRandomObject.nextInt(800) - 500;`
This works the same way for the rest of the "nextNumber" methods.

#5
6. Offline

### Quaro

This is one of the most craptastic codes I ever seen... Libarary with protected access, and methods that calls them self to keep generating code, instead of doing simple math they should... Not usable at all.

#6
7. Offline

#7
8. Offline

### sethrem

for pete's sake this code makes me cringe

#8
9. Offline

### Gorbit99

I can't decide on which one of the functions is my favorite, the ones where you just return a random value, or the ones, where you round a float, a double and a long.

#9