Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody!

im new in java and i got trouble with random in java.

i have list of data numbers:

1120,2014,3741,4563

and i want make random with these 4 numbers, but just random one of them.

ex: 3741.


i jave try this code:

Java
private static int skills;
	private static int getSkills() {
		switch(Rnd.get(skills)) {
		case 1120:
			skills= 1120;
			break;
		case 2014:
			skills= 2014;
			break;
                case 3741:
			skills= 3741;
			break;
		}
		return skills;
	}
Posted
Comments
[no name] 3-Sep-14 8:36am    
please specify your problem in brief

Put your data numbers inside an array, randomly select the array index and finally pick the array item corresponding to the selected index.
If you need to pick a different number at each extraction, then have a look at my tip: "Random extraction of 5 cards from a deck"[^] (I know it is C++, but here just the algorithm matters).
 
Share this answer
 
Java
public int getRandom() {
  int[] numbers = {1120,2014,3741,4563};
  Random rnd = new Random();
  int index = (int)(rnd.nextDouble() * 4.);
  return numbers[index];
}
 
Share this answer
 
i have a idea like this:

Java
private static final int[] skills = { 
		13016,
		13026,
	}; 

	private static int getSkillsForWeapon() {
		return Rnd.get(skills);
	}


this code will random one of two skills value?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900