generating random argument lists

M

Melody Class

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?
 
Y

Y. NOBUOKA

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
 
R

Robert Klemme

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

arrays =3D Array.new
rand(10).times do
=A0arr =3D Array.new
=A0rand(10).times { arr << rand(4) }
=A0arrays << arr
end

p arrays

Or:

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

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

Robert Klemme wrote in post #956010:

Or:

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

It seems there is a bracket misalignment. :)
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 =3D rand(10).times.map { rand(5).times.map { rand 4 } }

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
M

Melody Class

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
prefix the argument with a splat

my_method( *array_of_arrays )
 
B

Brian Candler

Robert Klemme wrote in post #956028:
It seems there is a bracket misalignment. :)

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) } }
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 :)
 
R

Robert Klemme

Robert Klemme wrote in post #956028:

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
I'm an old fogey and avoid avoid 1.8.7-isms :)

I really had to look up "fogey". Learn something new every day. :)

Cheers

robert
 
M

Melody Class

Josh Cheek 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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top