Support

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

For general discussion related FlowStone

unique random numbers ruby array

Postby tester » Mon Sep 22, 2014 4:57 pm

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)?
Need to take a break? I have something right for you.
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

Postby TheOm » Mon Sep 22, 2014 5:50 pm

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

Postby tester » Mon Sep 22, 2014 6:23 pm

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.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: unique random numbers ruby array

Postby billv » Mon Sep 22, 2014 9:52 pm

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
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: unique random numbers ruby array

Postby tester » Mon Sep 22, 2014 10:19 pm

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.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: unique random numbers ruby array

Postby billv » Mon Sep 22, 2014 11:30 pm

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.
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia


Return to General

Who is online

Users browsing this forum: No registered users and 84 guests