0

I am making a roulette game, I have created a Arraylist for the slots, they have been defined in a ordered list, there are 38 slots with positions (0-37), a color and number.

In "Spin method" I am trying to select a random starting slot from the wheel collection/list, then spin through some slots based on delay functionality.

How would I select a random slot from my collection to start this process?

My collection

    List<Slot> wheel = new ArrayList<Slot>();
    GameEngine gameEngine;

    public GameEngineImpl() {
        Color colorArray[] = new Color[] { Color.GREEN00, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED,
                Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED,
                Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.GREEN0, Color.BLACK, Color.RED,
                Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED,
                Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.RED };
        int numberArray[] = new int[] { 00, 27, 10, 25, 29, 12, 8, 19, 31, 18, 6, 21, 33, 16, 4, 23, 35, 14, 2, 0, 28,
                9, 26, 30, 11, 7, 20, 32, 17, 5, 22, 34, 15, 3, 24, 36, 13, 1 };
        for (int position = 0; position < 38; position++) {
            wheel.add(new SlotImpl(position, colorArray[position], numberArray[position]));
        }
    }

Spin method

@Override
    public void spin(int initialDelay, int finalDelay, int delayIncrement) {
        Slot slot;
        while (initialDelay < finalDelay) {

        //  TODO selecting a random starting slot on the wheel 


                }
            }

            // Delay function
            delay(delayIncrement);
            // Increase increment
            initialDelay += delayIncrement;
        }

    }
    // Method to delay spinning
    private void delay(int delay) {
        try {
            Thread.sleep(delay);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
  • 1
    "How do I do something *random* in Java" You do a web search for [`java random`](https://www.google.com/search?q=java+random), read the articles, and learn how. – Andreas Apr 07 '19 at 04:04

2 Answers2

2

You can use Random::nextInt for generating random integer:

int random = new java.util.Random().nextInt(38);
//nextInt: int value between 0 (inclusive) and the specified value (exclusive)

To iterate over the elements, you can either use stream or a for loop. Below is an example of stream:

wheel.subList(random, 37).stream().forEach(e -> {
    // e is the element
});

for loop:

for(int i = random; i < 38; i++) {
    var e = wheel.get(i); // e is the element
}
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
2

Two simple options:

  • use java.util.Random to generate a random int within the range of possible indexes of your array.
  • use Collections.shuffle() to simply put your list into random order and then pick the first entry each time.
GhostCat
  • 137,827
  • 25
  • 176
  • 248