组合多个数组的随机元素

时间:2021-12-30 21:27:27

In Ruby, how would I select a random element from one array and combine it with a randomly selected element from a second array?

在Ruby中,我如何从一个数组中选择一个随机元素并将其与第二个数组中随机选择的元素组合?

For example if I have the arrays:

例如,如果我有数组:

    array1 = ["x", "y", "z"]
    array2 = ["a", "b", "c"]

I'm looking for the output to be (array1_element)(array2_element)(array1_element) like xby or ycz or xbx so on and so forth. Can Ruby's .sample method be used on multiple arrays to print out a single string?

我正在寻找输出为(array1_element)(array2_element)(array1_element),如xby或ycz或xbx等等。可以在多个数组上使用Ruby的.sample方法打印出单个字符串吗?

3 个解决方案

#1


1  

There are a few ways to do this.

有几种方法可以做到这一点。

1) You can use sample method.

1)您可以使用样本方法。

array1.sample #=> return a random element from the array.

Then you can use string interpolation like

然后你可以使用字符串插值

result = "#{array1.sample}#{array2.sample}#{array3.sample}" #=> ie "xby"

2) You can generate random index values

2)您可以生成随机索引值

array1[rand(array1.length)]

This will generate a random index from 0 to length-1 and call the [] method on the array. You can then use string interpolation as well.

这将生成从0到length-1的随机索引,并在数组上调用[]方法。然后,您也可以使用字符串插值。

3) You can use the shuffle method then first method

3)你可以使用shuffle方法然后第一种方法

array1.shuffle.first

shuffle will, well, shuffle the array, and you can just return the first element.

shuffle会将数组洗牌,你可以返回第一个元素。

I'm just listing the few I can come up with off the top of my head. There are probably tons of other ways to do this.

我只是列出了一些我能想到的东西。可能有很多其他方法可以做到这一点。

#2


1  

How about

怎么样

(array1 + array2).sample(3)   # => ["b", "x", "a"]

if you want your answer in array form, or

如果你想以阵列形式回答,或者

(array1 + array2).sample(3).join('')   # => "zxb"

if you want it in string form?

如果你想以字符串形式?

If the order is supposed to explicitly be [array1_elt, array2_elt, array1_elt] and you don't want any element repeated, then the following should do the trick:

如果命令应该显式为[array1_elt,array2_elt,array1_elt]并且您不希望重复任何元素,那么以下应该可以解决这个问题:

array1.sample(2).insert(1, array2.sample)

Again, append .join('') to stringify the result.

再次,追加.join('')来对结果进行字符串化。

#3


0  

I assume you wish to form an array [a,b,c], where a and c are randomly selected from the first array and b is randomly selected from the second array, and then join the three elements of the array into a string.

我假设您希望形成一个数组[a,b,c],其中a和c是从第一个数组中随机选择的,b是从第二个数组中随机选择的,然后将数组的三个元素连接成一个字符串。

If you are sampling repeatedly it might be worthwhile to construct an array of all (3^3=27) combinations and then sample from that array:

如果您反复采样,可能值得构造一个包含所有(3 ^ 3 = 27)个组合的数组,然后从该数组中进行采样:

arr = array1.product(array2, array1).map(&:join)
  #=> ["xax", "xay", "xaz", "xbx", "xby", "xbz", "xcx", "xcy", "xcz",
  #    "yax", "yay", "yaz", "ybx", "yby", "ybz", "ycx", "ycy", "ycz",
  #    "zax", "zay", "zaz", "zbx", "zby", "zbz", "zcx", "zcy", "zcz"]

arr.sample #=> "zcx"
arr.sample #=> "yay"
arr.sample #=> "ycz" 
arr.sample #=> "xby" 

#1


1  

There are a few ways to do this.

有几种方法可以做到这一点。

1) You can use sample method.

1)您可以使用样本方法。

array1.sample #=> return a random element from the array.

Then you can use string interpolation like

然后你可以使用字符串插值

result = "#{array1.sample}#{array2.sample}#{array3.sample}" #=> ie "xby"

2) You can generate random index values

2)您可以生成随机索引值

array1[rand(array1.length)]

This will generate a random index from 0 to length-1 and call the [] method on the array. You can then use string interpolation as well.

这将生成从0到length-1的随机索引,并在数组上调用[]方法。然后,您也可以使用字符串插值。

3) You can use the shuffle method then first method

3)你可以使用shuffle方法然后第一种方法

array1.shuffle.first

shuffle will, well, shuffle the array, and you can just return the first element.

shuffle会将数组洗牌,你可以返回第一个元素。

I'm just listing the few I can come up with off the top of my head. There are probably tons of other ways to do this.

我只是列出了一些我能想到的东西。可能有很多其他方法可以做到这一点。

#2


1  

How about

怎么样

(array1 + array2).sample(3)   # => ["b", "x", "a"]

if you want your answer in array form, or

如果你想以阵列形式回答,或者

(array1 + array2).sample(3).join('')   # => "zxb"

if you want it in string form?

如果你想以字符串形式?

If the order is supposed to explicitly be [array1_elt, array2_elt, array1_elt] and you don't want any element repeated, then the following should do the trick:

如果命令应该显式为[array1_elt,array2_elt,array1_elt]并且您不希望重复任何元素,那么以下应该可以解决这个问题:

array1.sample(2).insert(1, array2.sample)

Again, append .join('') to stringify the result.

再次,追加.join('')来对结果进行字符串化。

#3


0  

I assume you wish to form an array [a,b,c], where a and c are randomly selected from the first array and b is randomly selected from the second array, and then join the three elements of the array into a string.

我假设您希望形成一个数组[a,b,c],其中a和c是从第一个数组中随机选择的,b是从第二个数组中随机选择的,然后将数组的三个元素连接成一个字符串。

If you are sampling repeatedly it might be worthwhile to construct an array of all (3^3=27) combinations and then sample from that array:

如果您反复采样,可能值得构造一个包含所有(3 ^ 3 = 27)个组合的数组,然后从该数组中进行采样:

arr = array1.product(array2, array1).map(&:join)
  #=> ["xax", "xay", "xaz", "xbx", "xby", "xbz", "xcx", "xcy", "xcz",
  #    "yax", "yay", "yaz", "ybx", "yby", "ybz", "ycx", "ycy", "ycz",
  #    "zax", "zay", "zaz", "zbx", "zby", "zbz", "zcx", "zcy", "zcz"]

arr.sample #=> "zcx"
arr.sample #=> "yay"
arr.sample #=> "ycz" 
arr.sample #=> "xby"