ID:96971
 
Keywords: dm, java, rand
rand.java
import java.util.Random;
public class rand {
public int rand(int x,int y){
if(y>x){
y -= x;
Random rval = new Random();
return x+rval.nextInt(y+1);
} else {
x -= y;
Random rval = new Random();
return y+rval.nextInt(x+1);
}
}
}


usage:
Testing.java
public class Testing{
public static void main(String args[]){
rand rnum = new rand();
for(int x=1;x<=10;x++){
int a = rnum.rand(50,40);
if(x==10){
System.out.println(a);
}else{
System.out.print(a + ", ");
}
}
System.out.println("Done!");
}
}
I'm thinking that either that rand() method should be static as you have no instance variables, or the internal Random class should be an instance variable, not a method local one. Creating Random instances can be quite expensive, because Java has to initialise a bunch of entropy for the algorithm, so perhaps the latter would be the better of the two.

In my opinion the ideal solution would be to extend Java's Random class, like so:

(caveat, completely untested)

http://pastebin.com/F3TL8Mza

package stephen001.byond.util;

import java.util.Random;

/**
* A class which extends Java's Random functionality, to provide inclusive
* bounds generation of integers, and inclusive range-based generation of
* integers.
*
* @author Stephen Badger
*/

public final class BYONDRandom extends Random
{
private static final long serialVersionUID = 1L;

/**
* Generates a random integer between min and max
* inclusive.
*
* @param min The minimum bound for the generated integer, inclusive.
* @param max The maximum bound for the generated integer, exclusive.
* @return An integer between min and max inclusive.
*/

public int nextInt( int min, int max ) {
if ( min > max ) {
int intermediary = max;
max = min;
min = intermediary;
}
return min + nextInt( max - min );
}

/* (non-Javadoc)
* @see java.util.Random#nextInt(int)
*/

@Override
public int nextInt( int max ) {
return super.next( max + 1 );
}
}


The usage is then exactly the same as Java's Random class, but the nextInt() method is inclusive like DM's rand(), and you have a nextInt() method that permits an inclusive range, again like DM's rand().
Hmm, I'm just now beginning to learn java so I'm still not clear on all of these final / static things yet ^^; Trying not to rush myself into it like I did with some other languages *cough* C++. Which i lost all motivation to learn. Thanks though, when I learn more about Java I'll check back and re-read what you have posted.
Leur wrote:
Hmm, I'm just now beginning to learn java so I'm still not clear on all of these final / static things yet ^^; Trying not to rush myself into it like I did with some other languages *cough* C++. Which i lost all motivation to learn. Thanks though, when I learn more about Java I'll check back and re-read what you have posted.

I won't explain final because it's a little complicated, I'll leave that for your reading.

Static (on methods) is fairly simple, you don't need an object to use the method. A good example is Math.random():

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ Math.html#random%28%29

// Instead of this:
Math math = new Math();
double result = math.random();

// You can simply do this:
double result = Math.random();


The downside is obviously you can't access object variables in a static method.