PHP array shuffle and array_rand replacement

Working on card game deck, where want to get (or sometimes take) random card and also mix the array. In PHP, it's simple by array_rand and shuffle functions but what about Rust?

Found few options, where not sure about performance context - maybe the vector even is not optimal choose here as keep the indexes order and maybe I want some other kind of array type..

It's probably not a good idea to post AI output here and ask for others to look at it. Posting machine generated content is not allowed in general.

rand is probably the crate you want. Here's the trait that probably fulfills your needs.

4 Likes

Sorry, just removed (I'm using it instead of Google last time) about machine generated content it's maybe something about bots activity, as people play with that everywhere. But will keep in mind!

rand is probably the crate you want. Here's the trait that probably fulfills your needs.

Thank you! Have done with following implementation:

pub fn rand(&self) -> Option<&T> {
    self.vector.choose(&mut thread_rng())
}

pub fn shuffle(&mut self) {
    self.vector.shuffle(&mut thread_rng());
}