0
Java - Accessing multiple random array elements
I would like to access array elements randomly in a boolean array with a random number from 1 to 3 because I would like to develop a simulation for my assignment that allows the user to select 1 to 3 seats randomly in a theatre system and randomly selects the index in the array and set it to true to indicate that it is booked. May I know is it possible to do so? If there is, can someone please help me out…thanks!
3 ответов
+ 3
Should be possible. You just need an array and then generate the random number "n" from 0 - 2 (3 seats) and put that in the array[n].
Check out https://www.geeksforgeeks.org/generating-random-numbers-in-java/
+ 1
(int)(Math.random()*3) => Returns 0,1, or 2
+ 1
Thanks alot!