phamousphil
phamousphil

Reputation: 254

How to return 5 random "Powerball" numbers in Java

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.

Upvotes: 3

Views: 2704

Answers (7)

SpYk3HH
SpYk3HH

Reputation: 22580

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!

Upvotes: 0

Walery Strauch
Walery Strauch

Reputation: 7072

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;
}

Upvotes: 1

Rahul
Rahul

Reputation: 45080

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);

Upvotes: 2

karthick
karthick

Reputation: 12176

It's a straight forward approach.

   List<Integer> generated = new ArrayList<Integer>();
          for (int i = 0; i < 5; i++)
          {
              while(true)
              {
                 int r = (int) (Math.random() * 42 + 1);
                  if (!generated.contains(r))
                  {            
                      generated.add(r);
                      break;
                  }
              }
          }

Upvotes: 0

nommyravian
nommyravian

Reputation: 1336

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"

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37576

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);
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

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;
}

Upvotes: 2

Related Questions