Random Picker

N

nialltimpson

I'm trying to write a program that randomily 1 of 6 possible numbers
(predefined), I was thinking of using math.random, to choose a number
between 0 and 1. Then dividing this range into 6, corresponding to the
predefinid number. But It just sound like such an adhoc way of doing it.
Any better idea's?

Niall
 
S

ShoX

-------------------------------------
import java.util.Random;

int randnum;
Random generator = new Random();

randnum = generator.nextInt(6);
--------------------------------------

Hopefully that's what you wanted. java.util.Random is pretty good for
getting randoms.

Chuck
http://shox.teklore.org
 
J

jr

Maybe something such as this would give you what you want.

######################
import java.io.*;

public class RandomNumber {

public RandomNumber() {};

public int getRandomNumber(int scale) {
return 1+(int)(scale*Math.random());
}

}
######################

######################
import java.util.*;

public class tryRandomNumber {
public static void main(String[] args) {
RandomNumber r = new RandomNumber();
int maxRandomNumber = 6;
System.out.println(
"Random number: "+
r.getRandomNumber(maxRandomNumber)
);

}
}
######################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top