Name | Code (Block) | Code (Text) |
---|---|---|
Random List Access Pattern | ![]() | ![]() |
How does it work?
Many App Lab projects run in the following way.
- The user is interested in seeing items in a list, like a list of favorite foods, or a list of reminders.
- The app shows the user one item in the list at a time.
- By clicking buttons (or otherwise interacting with the app) the a new random item in the list is shown to the user.
This pattern allows a user to see random elements in a list. In order to make it work, create an index variable and a list. Then generate a random number between 0 and the length of the list minus one and set the index to that value. Then update the screen.
Found a bug in the documentation? Let us know at support@code.org.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.