Java - Accessing multiple random array elements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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!

17th Jul 2022, 1:40 PM
Andrew Yap Yuntze
Andrew Yap Yuntze - avatar
3 Answers
+ 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/
17th Jul 2022, 1:50 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
(int)(Math.random()*3) => Returns 0,1, or 2
17th Jul 2022, 1:52 PM
Jayakrishna 🇮🇳
+ 1
Thanks alot!
17th Jul 2022, 9:26 PM
Andrew Yap Yuntze
Andrew Yap Yuntze - avatar