Generating random argument lists

Hi,
I have a method that takes a variable length argument list of arrays
i.e.
def meth(*list_of_arrays)
end

now i just want to randomly generate a random number of arrays of random
lengths to pass as arguments to that method (each array being of
integers 0 to 3)

any suggestions?

now i just want to randomly generate a random number of arrays of random
lengths to pass as arguments to that method (each array being of
integers 0 to 3)

any suggestions?

One way is to use the built-in function rand.
Here is an example program:

arrays = Array.new
rand(10).times do
arr = Array.new
rand(10).times { arr << rand(4) }
arrays << arr
end

p arrays

On Thu, Oct 21, 2010 at 8:11 AM, Y. NOBUOKA [email protected]
wrote:

rand(10).times do
arr = Array.new
rand(10).times { arr << rand(4) }
arrays << arr
end

p arrays

Or:

arrays = Array.new rand 10 do
Array.new(rand 5) { rand 4 }
end

Kind regards

robert

On Thu, Oct 21, 2010 at 10:39 AM, Brian C. [email protected]
wrote:

Or:

arrays = Array.new rand 10 do
Array.new(rand 5) { rand 4 }
end

Or:

arrays = (1…rand(10)).map { (1…rand(5).map { rand(4)) } }

It seems there is a bracket misalignment. :slight_smile:

That also gives between 0 and 9 arrays, each with between 0 and 4
elements, each element being between 0 and 3.

Note that rand(x) can return 0 so it would be more natural to start
ranges at 0 IMHO.

Another version with less brackets especially around ranges where I
find them ugly:

arrays = rand(10).times.map { rand(5).times.map { rand 4 } }

Cheers

robert

Robert K. wrote in post #956010:

On Thu, Oct 21, 2010 at 8:11 AM, Y. NOBUOKA [email protected]domain.invalid
wrote:

rand(10).times do
arr = Array.new
rand(10).times { arr << rand(4) }
arrays << arr
end

p arrays

Or:

arrays = Array.new rand 10 do
Array.new(rand 5) { rand 4 }
end

Kind regards

robert

Or:

arrays = (1…rand(10)).map { (1…rand(5).map { rand(4)) } }

That also gives between 0 and 9 arrays, each with between 0 and 4
elements, each element being between 0 and 3.

On Thu, Oct 21, 2010 at 8:34 AM, Melody Class
[email protected]wrote:


Posted via http://www.ruby-forum.com/.

prefix the argument with a splat

my_method( *array_of_arrays )

thanks, the elegance of these is pleasing. question remains: how to i
split that array of arrays into a list of arrays to pass to the method?
if i don’t split it then my method enumerates not through an array of
arrays but through an array with only one member: the array of arrays.
In the end i refactored the method’s code to say
if *args.length==1 and args[0].class==Array then args[0].each do x else
args.each do x

but I suppose I’m curious as to whether it’s possible to generate a list
of arguments

Robert K. wrote in post #956028:

arrays = (1…rand(10)).map { (1…rand(5).map { rand(4)) } }

It seems there is a bracket misalignment. :slight_smile:

Sorry, I corrected it in irb but forgot to paste it back into the
message I was composing.

arrays = (1…rand(10)).map { (1…rand(5)).map { rand(4) } }

That also gives between 0 and 9 arrays, each with between 0 and 4
elements, each element being between 0 and 3.

Note that rand(x) can return 0 so it would be more natural to start
ranges at 0 IMHO.

I did that originally, but (0…0).map { … } gives a one-element array,
and I wanted to have empty arrays too. You can omit the far end with
(0…0), but then (0…rand(5)) gives between 0 and 3, which is even
more confusing IMO.

Another version with less brackets especially around ranges where I
find them ugly:

arrays = rand(10).times.map { rand(5).times.map { rand 4 } }

I’m an old fogey and avoid avoid 1.8.7-isms :slight_smile:

Josh C. wrote in post #956077:

prefix the argument with a splat

my_method( *array_of_arrays )

ah I see… the splat either breaks an array into a list or pulls the
list into an array, depending whether it’s included in the def or the
call

so

def my_method(*array_of_arrays) #takes a list
array_of_arrays.each {|a| print a}
end

my_method(1,2,3) #=> 123
a=[1,2,3]
my_method(*a) #=> 123
my_method(a) #=>[1,2,3]

On Thu, Oct 21, 2010 at 5:27 PM, Brian C. [email protected]
wrote:

Robert K. wrote in post #956028:

That also gives between 0 and 9 arrays, each with between 0 and 4
elements, each element being between 0 and 3.

Note that rand(x) can return 0 so it would be more natural to start
ranges at 0 IMHO.

I did that originally, but (0…0).map { … } gives a one-element array,
and I wanted to have empty arrays too. You can omit the far end with
(0…0), but then (0…rand(5)) gives between 0 and 3, which is even
more confusing IMO.

No, (0…rand(5)).map… will create arrays with 0 to 4 elements. I
find that totally natural

irb(main):010:0> 5.times do |rnd|
irb(main):011:1* printf “%2d %p\n”,rnd,(0…rnd).map{“x”}
irb(main):012:1> end
0 []
1 [“x”]
2 [“x”, “x”]
3 [“x”, “x”, “x”]
4 [“x”, “x”, “x”, “x”]
=> 5

Another version with less brackets especially around ranges where I
find them ugly:

arrays = rand(10).times.map { rand(5).times.map { rand 4 } }

I’m an old fogey and avoid avoid 1.8.7-isms :slight_smile:

I really had to look up “fogey”. Learn something new every day. :slight_smile:

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs