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方法 。 大家都必须认为该方法必须执行与Array实例中的元素或对象的改组有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

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.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 该方法的工作方式是随机地对Array实例中存在的对象进行洗牌。 此方法的返回类型是一个Array对象,它以随机的方式包含self的所有元素。 您还可以提供一个可选参数rng ,可用作随机数生成器。 此方法是非破坏性方法的示例之一,这意味着此方法创建的更改不是永久的或临时的,并且不会影响self Array实例中元素的实际排列。

Syntax:

句法:

    array_instance.shuffle -> new_array
or
array_instance.shuffle(random:rng)-> new_array

Argument(s) required:

所需参数:

This method takes one argument which is optional. This argument can be used for random number generation.

此方法采用一个可选参数。 此参数可用于生成随机数

Example 1:

范例1:

=begin
Ruby 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
puts "Array instance after shuffling: #{pq}"
puts "Array instance:"
print table 

Output

输出量

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:

说明:

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方法 ,我们正在对Array实例中的元素进行改组 。 您可以观察到,在两次运行中,由于元素的改组始终是随机的,因此输出或生成的Array实例是不同的。 您还可以看到self Array中的元素保持不变,因为此方法是非破坏性方法的示例之一。

Example 2:

范例2:

=begin
Ruby 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 table 

Output

输出量

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:

说明:

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方法对Array实例的元素进行混洗 。 我们在方法内部传递参数以生成随机数。 这以使改组为常数的方式帮助您。 在这两次运行中,您都可以观察到返回的Array是常量。 此方法是一种非破坏性方法,这就是为什么它不会在实际的数组实例中创建任何更改的原因。

翻译自: https://www.includehelp.com/ruby/array-shuffle-method-with-example.aspx

ruby array

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541088.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【147天】尚学堂高淇Java300集视频精华笔记(108-109)

第108集:容器equals和hashcodeJDK源代码分析 本集知识点 Java中规定,若两个对象equals比较后内容相等(为true),则hashCode必须相等,反之不然。【原因见内存分析图】hashCode与equals方法必须同时重写,且必须…

ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

ruby hash方法Hash.key?(obj)方法 (Hash.key?(obj) Method) In this article, we will study about Hash.key?(obj) Method. The working of the method cant be assumed because of its quite a different name. Let us read its definition and understand its …

python迭代器与生成器答案_史上最全 Python 迭代器与生成器

原标题:史上最全 Python 迭代器与生成器作者:浪子燕青链接:http://www.langzi.fun/迭代器与生成器.html迭代器与可迭代对象概念迭代器:是访问数据集合内元素的一种方式,一般用来遍历数据,但是他不能像列表一…

[性能测试] LoadRunner结果分析 – TPS

本文转载自:http://www.tuicool.com/articles/6z6vuy针对吞吐率和 TPS 的关系,这个在结果分析中如何使用,就个人经验和朋友讨论后,提出如下建议指导,欢迎同僚指正。相关定义响应时间 网络响应时间 应用程序响应时间响…

密码学电子书_密码学中的电子密码书(ECB)

密码学电子书This Electronic Code Book (ECB) is cryptography as a mode of operation for a block cipher, with the characters the main things that every feasible block of plaintext or an original text has a corresponding characteristic of ciphertext value and…

tsql是mysql中的吗_Mysql中的sql是如何执行的

MySQL中的SQL是如何执行的MySQL是典型的C/S架构,也就是Client/Server架构,服务器端程序使用的mysqld.整体的MySQL流程如下图所示:MySQL是有三层组成:连接层: 负责客户端与服务器端建立连接,客户端发送SQL至服务端;SQL层: 对SQL语句进行查询处理;存储引擎层: 与数据库文件打交道…

软件质量特性测试

针对软件质量特性进行测试,可以避免重大漏测,一般人我不告诉他。《软件工程—产品质量》(GB/T 16260-2006)中规定对软件的每个质量特性与子特性都有定义:一、功能性:是指当软件在指定条件下使用&#xff0c…

PHP array_pop()函数与示例

PHP array_pop()函数 (PHP array_pop() function) array_pop() function is used to delete/pop last element from the array. array_pop()函数用于从数组中删除/弹出最后一个元素。 Syntax: 句法: array_pop(array);Here, array is the input array, function w…

网站关停就没事了?5100万账户文件被盗

曾经是美国三大音乐视频文件共享软件之一的imesh,意外倒闭。而更意外的是,就在近日,imesh这款已经倒闭的软件,5100万账户开始在暗网被黑客拍卖。 Imesh这款软件是美国纽约的老牌音乐视频分享软件之一,早在2000年前便已…

数据库表设计索引外键设计_关于索引的设计决策 数据库管理系统

数据库表设计索引外键设计Introduction: 介绍: The attributes whose values are required inequality or range conditions and those that are keys or that participate in join conditions require access paths. 其值为必需的不等式或范围条件的属性以及作为键…

接口测试从零开始系列_mock技术使用

1、什么情况下会使用mock技术 (1)需要将当前被测单元和其依赖模块独立开来,构造一个独立的测试环境,不关注被测单元的依赖对象,只关注被测单元的功能逻辑 ----------比如被测代码中需要依赖第三方接口返回值进行逻辑处…

amie 规则挖掘_AMIE的完整形式是什么?

amie 规则挖掘AMIE:工程师协会的准会员 (AMIE: Associate Member of the Institution of Engineers) AMIE is an abbreviation of Associate Member of the Institution of Engineers. The Institution of Engineers India Limited (IEIL) provides this profession…

java 马克思_单链表-Java

public class SinglyListNode {int val;SinglyListNode next;SinglyListNode() {}SinglyListNode(int x) {this.val x;}}/*执行用时:12 ms, 在所有 Java 提交中击败了66.93%的用户内存消耗:39.5 MB, 在所有 Java 提交中击败了5.06%的用户*/class MyLink…

python的pass语句_Python | 演示pass语句的示例

python的pass语句python中的pass语句 (pass statement in python) "pass" is a type of null operation or null statement, when it executes nothing happens. It is used when you want do not want to write any code/statement to execute but syntactically a …

HDS:聚焦未来的投资“冻结”

一家日本IT网站报道的有关HDS冻结对高端存储产品的投资一事引发众议。让人陷入疑惑的这次声明就是,HDS认为单纯的阵列产品并非企业存储的未来。 6月1日,IT Pro Nikkei网站发布了一篇报道,内容援引HDS一份表示将冻结高端存储业务的简报。这引发…

java js对象转字符串数组_JS数组转字符串(3种方法)【转】

JavaScript 允许数组与字符串之间相互转换。其中 Array 方法对象定义了 3 个方法,可以把数组转换为字符串,如表所示。数组方法说明toString()将数组转换成一个字符串toLocalString()把数组转换成本地约定的字符串join()将数组元素连接起来以构建一个字符…

中美共建大数据创新研究中心

由贵阳市人民政府、工信部电子一所、美国加州大学伯克利分校合作共建的贵州伯克利大数据创新研究中心日前在贵阳揭牌。 据了解,贵州伯克利大数据创新研究中心将分两阶段建设。第一阶段,2016年9月份至2017年底,将重点完成“学龄儿童大数据分析…

Python中的__init__和self是做什么的?

The __init__ and self are two keywords in python, which performs a vital role in the application. __init__和self是python中的两个关键字,在应用程序中起着至关重要的作用。 To begin with, it is important to understand the concept of class and object…

Palo Alto Networks漏洞防护扩展至云端

中国北京,2016年4月12日 –下一代安全企业Palo Alto Networks?(纽交所代码:PANW)近日宣布进一步增强其下一代安全平台,扩展漏洞防护能力,以满足那些依赖云环境和SaaS应用的业务对安全的需求。 企业机构需要变得更加灵活和有竞争力…

java 嵌套调用_Java嵌套类的使用

嵌套类是指被定义在另一个类内部的类,它为外部类提供服务。嵌套类分四种:静态成员类、非静态成员类、匿名类和局部类。一、静态成员类与非静态成员类的区别?在什么情况下可以用静态成员类?我们知道在类的设计中,为了避…