If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
unique random numbers ruby array
6 posts
• Page 1 of 1
unique random numbers ruby array
I know I have it somewhere, but can't find it.
How to quickly create in ruby - an array of specified size (how many items), made of unique random numbers (integers) in specified range (from..to)?
How to quickly create in ruby - an array of specified size (how many items), made of unique random numbers (integers) in specified range (from..to)?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: unique random numbers ruby array
There's probably a better way, but I would do
- Code: Select all
(from..to).to_a.shuffle.take(size)
- TheOm
- Posts: 103
- Joined: Tue Jan 28, 2014 7:35 pm
- Location: Germany
Re: unique random numbers ruby array
Should be fine, thanks. I just need to pick some random graphic files from folder, to get different results per each click.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: unique random numbers ruby array
Sounds like you won't need this tester, but I will add it for sake of the 'thread title".
The array version of my Urn module will ensure every element remains unique
and non-repeating with every shuffle.
http://www.dsprobotics.com/support/viewtopic.php?f=3&t=2380#p14120
The array version of my Urn module will ensure every element remains unique
and non-repeating with every shuffle.
http://www.dsprobotics.com/support/viewtopic.php?f=3&t=2380#p14120
BV MUSIC SYDNEY AUSTRALIA..Songwriting and Software development
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
- billv
- Posts: 1157
- Joined: Tue Aug 31, 2010 3:34 pm
- Location: Australia
Re: unique random numbers ruby array
What's wrong with what TheOm provided? It works, it's single line, and is resistant to borderline situations as far I can see (pool size vs declaration array size).
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: unique random numbers ruby array
tester wrote:What's wrong with what TheOm provided?
Where did i say it was wrong?
Like i said tester..
billv wrote:Sounds like you won't need this tester, but I will add it for sake of the 'thread title"
As I wrote, the behaviour of my Urn array is different, and might be useful for someone
who see's this thread title, and seeking a alternative option.
BV MUSIC SYDNEY AUSTRALIA..Songwriting and Software development
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
- billv
- Posts: 1157
- Joined: Tue Aug 31, 2010 3:34 pm
- Location: Australia
6 posts
• Page 1 of 1
Who is online
Users browsing this forum: Google [Bot] and 76 guests