Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example that shows off LoadRandomSequence #3846

Merged
merged 2 commits into from
Feb 29, 2024
Merged

Add example that shows off LoadRandomSequence #3846

merged 2 commits into from
Feb 29, 2024

Conversation

REDl3east
Copy link
Contributor

This example shows off what can be done with the LoadRandomSequence function. The program generates a random sequence of colored rectangles. You can add a rectangle and regenerate by pressing the UP key, remove a rectangle and regenerate with the DOWN key, and to shuffle the rectangles with the SPACE key.

I had a need to do some simple shuffling in a game I was making and was surprised there was no example already. It also made me think of a possible addition to raylib, that would be some sort of shuffle function. Shuffling is something lots of games use and would be beneficial to the library. It would look something a lot like this: RLAPI void ShuffleRandomSequence(int *sequence, unsigned int count);. It would already be beneficial to this example because every time I would want to shuffle I would need to call another LoadRandomSequence, which allocates more memory than it needs to.

@REDl3east REDl3east changed the title Add example that shows of LoadRandomSequence Add example that shows off LoadRandomSequence Feb 29, 2024
@orcmid
Copy link
Contributor

orcmid commented Feb 29, 2024

There's a more direct way to shuffle the members of a list that does not requires LoadRandomSequence() and the creation and release of a transient array.

You may find it to be great fun for your purpose. It is given as Algorithm P (Shuffling) in section 3.4.2 in The Art of Computer Programming, volume 2: Seminumerical Algorithms.

Conversion to C Language is relatively straightforward.

@raysan5 raysan5 merged commit 077ab6d into raysan5:master Feb 29, 2024
@raysan5
Copy link
Owner

raysan5 commented Feb 29, 2024

@REDl3east Thanks for the addition! I'll review it to minimize static functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants