ruby array_Ruby中带有示例的Array.shuffle方法
ruby array
Array.shuffle方法 (Array.shuffle Method)
In this article, we will study about Array.shuffle method. You all must be thinking the method must be doing something which is related to shuffling of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我們將研究Array.shuffle方法 。 大家都必須認(rèn)為該方法必須執(zhí)行與Array實(shí)例中的元素或?qū)ο蟮母慕M有關(guān)的操作。 它并不像看起來(lái)那么簡(jiǎn)單。 好吧,我們將在其余內(nèi)容中解決這個(gè)問(wèn)題。 我們將嘗試借助語(yǔ)法并演示程序代碼來(lái)理解它。
Method description:
方法說(shuō)明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it shuffles the objects present in the Array instance randomly. The return type of this method is an Array object which contains all the elements of self in a shuffled manner. You can also provide an optional argument rng which can be used as a random number generator. This method is one of the examples of non-destructive method which means that the changes created by this method are not permanent or temporary and would not impact the actual arrangement of elements in the self Array instance.
該方法是一個(gè)公共實(shí)例方法,為Ruby庫(kù)中的Array類定義。 該方法的工作方式是隨機(jī)地對(duì)Array實(shí)例中存在的對(duì)象進(jìn)行洗牌。 此方法的返回類型是一個(gè)Array對(duì)象,它以隨機(jī)的方式包含self的所有元素。 您還可以提供一個(gè)可選參數(shù)rng ,可用作隨機(jī)數(shù)生成器。 此方法是非破壞性方法的示例之一,這意味著此方法創(chuàng)建的更改不是永久的或臨時(shí)的,并且不會(huì)影響self Array實(shí)例中元素的實(shí)際排列。
Syntax:
句法:
array_instance.shuffle -> new_arrayorarray_instance.shuffle(random:rng)-> new_arrayArgument(s) required:
所需參數(shù):
This method takes one argument which is optional. This argument can be used for random number generation.
此方法采用一個(gè)可選參數(shù)。 此參數(shù)可用于生成隨機(jī)數(shù) 。
Example 1:
范例1:
=beginRuby program to demonstrate shuffle method =end# array declaration table = [2,4,6,8,10,12,14,16,18,20]puts "Array shuffle implementation" pq =table.shuffleputs "Array instance after shuffling: #{pq}"puts "Array instance:" print tableOutput
輸出量
RUN 1: Array shuffle implementation Array instance after shuffling: [14, 18, 12, 16, 6, 4, 2, 10, 8, 20] Array instance: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]RUN 2: Array shuffle implementation Array instance after shuffling: [12, 14, 18, 2, 20, 10, 6, 4, 16, 8] Array instance: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]Explanation:
說(shuō)明:
In the above code, you can observe that we are shuffling the elements from the Array instance with the help of Array.shuffle method. You can observe that in both the runs, the output or the Array instance generated is different because the shuffling of the elements is always random. You can also see that the elements in self Array remain unchanged because this method is one of the examples of the non-destructive methods.
在上面的代碼中,您可以觀察到借助Array.shuffle方法 ,我們正在對(duì)Array實(shí)例中的元素進(jìn)行改組 。 您可以觀察到,在兩次運(yùn)行中,由于元素的改組始終是隨機(jī)的,因此輸出或生成的Array實(shí)例是不同的。 您還可以看到self Array中的元素保持不變,因?yàn)榇朔椒ㄊ欠瞧茐男苑椒ǖ氖纠弧?
Example 2:
范例2:
=beginRuby program to demonstrate shuffle method =end# array declaration table = [2,4,6,8,10,12,14,16,18,20]puts "Array shuffle implementation" pq = table.shuffle(random: Random.new(2))puts "Array instance after shuffling: #{pq}"puts "Array instance:" print tableOutput
輸出量
RUN 1: Array shuffle implementation Array instance after shuffling: [10, 4, 12, 2, 16, 6, 8, 14, 20, 18] Array instance: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]RUN 2: Array shuffle implementation Array instance after shuffling: [10, 4, 12, 2, 16, 6, 8, 14, 20, 18] Array instance: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]Explanation:
說(shuō)明:
In the above code, you can observe that we are shuffling the elements of the Array instance with the help of Array.shuffle method. We are passing an argument inside the method in order to generate a random number. This helps you in the way that it makes the shuffling constant. In both the runs, you can observe that the returning Array is constant. This method is a non-destructive method that is why it is not creating any changes in the actual array instance.
在上面的代碼中,您可以觀察到我們正在借助Array.shuffle方法對(duì)Array實(shí)例的元素進(jìn)行混洗 。 我們?cè)诜椒▋?nèi)部傳遞參數(shù)以生成隨機(jī)數(shù)。 這以使改組為常數(shù)的方式幫助您。 在這兩次運(yùn)行中,您都可以觀察到返回的Array是常量。 此方法是一種非破壞性方法,這就是為什么它不會(huì)在實(shí)際的數(shù)組實(shí)例中創(chuàng)建任何更改的原因。
翻譯自: https://www.includehelp.com/ruby/array-shuffle-method-with-example.aspx
ruby array
總結(jié)
以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.shuffle方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [转载] Python中的解析式和生成器
- 下一篇: ScalavsKotlin