DM's rand() through Java! in Off Topic
|
|
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!"); } }
|
|
In my opinion the ideal solution would be to extend Java's Random class, like so:
(caveat, completely untested)
http://pastebin.com/F3TL8Mza
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().