Q:

Write a Ruby program to pick number of random elements from a given array

0

Write a Ruby program to pick number of random elements from a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

nums = [12, 34, 23, 56]
print "Original array:\n"
print nums
print "\n 2 random elements from the array.\n"
print nums.sample(2)
print "\n 3 random elements from the array.\n"
print nums.sample(3)

Output:

Original array:
[12, 34, 23, 56]
 2 random elements from the array.
[34, 12]
 3 random elements from the array.
[56, 12, 34]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now