How to return 5 random “Powerball” numbers in Java

2019-07-04 04:44发布

Trying to return 5 random numbers between 1 and 42 in Java.

I currently have logic to return a single number (putting it into an ArrayList, but I'd like to do away with that.) I'm stumped on implementation to return 5 random numbers. Would I need 5 for loops?

    for (int i = 0; i < 10; i++) {
        int r = (int) (Math.random() * 42 + 1);
    }

I've seen some other related examples here and they seem more complex than what my needs dictate. However, I could be wrong.

7条回答
地球回转人心会变
2楼-- · 2019-07-04 05:23

Store the numbers in array and return that array.

int []randArray;
randArray = new int[5];

for (int i = 0; i < 5; i++) { //for 5 random numbers
        randArray[i] = (int) (Math.random() * 42 + 1);
    }

//now return this array "randArray"
查看更多
老娘就宠你
3楼-- · 2019-07-04 05:23

Just throwing my 2 cents in. I recently made a jQuery Plugin, appropriately named "Powerball". I'll share with ya the formula i'm using as well as link ya my plugin. Not sure why anyone would really need this. I did it just for fun! LoL!

The Function

function getNumbers() {
    var a=[];   //  array to return
    for(i=1;5>=i;i++){  //  for loop to get first 5 numbers
        var b = Math.floor(Math.random()*59)+1; //  set #
        while (a.indexOf(b) > -1) { b = Math.floor(Math.random()*59)+1; }   //  reset # if already used
        a.push(b);  //  add number to array
    }
    a.push(Math.floor(35*Math.random())+1); //  add ball 6 number
    return a;   //  0 index array will have a length of 6, [0-4] being whiteball numbers, [5] being the red ball
}

The Plugin

jsFiddle

  • Contains the plugin between comment lines
  • Shows example use

Use is as easy as $("element").powerball(). However, only one method exist for it at the moment, $("element").powerball("setNumbers"). That method simply resets the numbers shown in the p tags.

  • Style: a note!
    • All styling is done through a style tag added to the header upon initialization. This means there's no need for extra files to add, but it also gives the ease of custom styling. See more about styling in the jsFiddle!
查看更多
干净又极端
4楼-- · 2019-07-04 05:29

Try it like this:

IntArray = new int[5]; //Create an array
for (int i = 0; i < 5; i++) {
        IntArray[i] = (int) (Math.random() * 42 + 1);
}
查看更多
时光不老,我们不散
5楼-- · 2019-07-04 05:30

Simply place each random number into an array and return the array...

public int[] powerBalls() {
     int[] balls = new int[5];
     for (int index = 0; index < 5; index++) {
          balls[index] = (int) (Math.random() * 42) + 1;
     }
     return balls;
}
查看更多
Lonely孤独者°
6楼-- · 2019-07-04 05:31

Be careful! Each number can be taken only one time. With your solution it is possible to get same number more than one time.

Other solution (and here you can't have same numer more than one time) is to create array with all numbers, shuffle it and take first 5:

public int[] powerBalls() {
  // create array with all numbers
  List<Integer> balls = new ArrayList<Integer>(42);
  for (int i = 1; i <= 42; i++)
    balls.add(i);

  // shuffle
  Collections.shuffle(balls);

  // take first 5
  int[] result = new int[5];
  for (int i = 0; i < 5; i++)
    result[i] = balls.get(i);

  return result;
}
查看更多
Explosion°爆炸
7楼-- · 2019-07-04 05:41

You can use the Set to generate 5 Unique Random numbers.

Random random = new Random();
Set randomNumbers = new HashSet<Integer>();
while(randomNumbers.size()< 5) {
    randomNumbers.add(random.nextInt(42)+1);
}

Since you've mentioned that you're using an ArrayList which will hold all the random numbers, you could just add all the elements present in randomNumbers set to your ArrayList.

Update:-

To suit your needs, you need to do something like this:-

Random random = new Random();
Set<String> set = new HashSet<String>();
while(set.size()< 5) {
    set.add(String.valueOf(random.nextInt(42)+1));
}
fortuneList3.addAll(set);
查看更多
登录 后发表回答