I’m using Array.sample to return a random element from an array.
I want to then get the index of that element to generate a permalink. Is there a built in method to do this? Or will I need to roll my own?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think there is a pre-baked solution but you can make your own easily enough. You could use
sampleon the indices:That gives you both the index,
i, and the sampled element,e. Or, since you’re just extracting a sample of size one, useranddirectly and skip all thesamplenoise: