in Education by
This is what I have so far: myArray.map!{ rand(max) } Obviously, however, sometimes the numbers in the list are not unique. How can I make sure my list only contains unique numbers without having to create a bigger list from which I then just pick the n unique numbers? Edit: I'd really like to see this done w/o loop - if at all possible. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
(0..50).to_a.sort{ rand() - 0.5 }[0..x] (0..50).to_a can be replaced with any array. 0 is "minvalue", 50 is "max value" x is "how many values i want out" of course, its impossible for x to be permitted to be greater than max-min :) In expansion of how this works (0..5).to_a ==> [0,1,2,3,4,5] [0,1,2,3,4,5].sort{ -1 } ==> [0, 1, 2, 4, 3, 5] # constant [0,1,2,3,4,5].sort{ 1 } ==> [5, 3, 0, 4, 2, 1] # constant [0,1,2,3,4,5].sort{ rand() - 0.5 } ==> [1, 5, 0, 3, 4, 2 ] # random [1, 5, 0, 3, 4, 2 ][ 0..2 ] ==> [1, 5, 0 ] Footnotes: It is worth mentioning that at the time this question was originally answered, September 2008, that Array#shuffle was either not available or not already known to me, hence the approximation in Array#sort And there's a barrage of suggested edits to this as a result. So: .sort{ rand() - 0.5 } Can be better, and shorter expressed on modern ruby implementations using .shuffle Additionally, [0..x] Can be more obviously written with Array#take as: .take(x) Thus, the easiest way to produce a sequence of random numbers on a modern ruby is: (0..50).to_a.shuffle.take(x)

Related questions

0 votes
    In Ruby, Dir.glob("**/*.rb") (for instance) doesn't traverse symlinked directories. Is it ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    first..sorry for my poor english and my big noob question..the problem is... I need create a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    As the title suggests, I'm just looking for a way of pressing a button in Shoes without clicking ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    This question exist only because of pure curiosity. Not a homework. Find the fastest way to find two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I declare steps that need to be done in order to install wordpress in an array in the beginning of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    Let's say I have one variable, directory_list, which I define and set in a ruby_block named get_directory_list. Can ... woohoo" end end Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am trying to generate 10,000 unique random integers in the range of 1 to 20,000 to store in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I am trying to generate 10,000 unique random integers in the range of 1 to 20,000 to store in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    How to generate real random numbers having as low and high 2 real numbers? I want random numbers between: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    How to generate real random numbers having as low and high 2 real numbers? I want random numbers between: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    How to generate real random numbers having as low and high 2 real numbers? I want random numbers between: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    How to generate real random numbers having as low and high 2 real numbers? I want random numbers between: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
+1 vote
    What is the process to generate random numbers in C programming language?...
asked Nov 8, 2020 in Technology by JackTerrance
...