ruby array

Array.sample()方法 (Array.sample() Method)

In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. 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.sample()方法 。 大家都必须认为该方法必须做的事情与我们研究的所有这些方法截然不同。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

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 chooses a random object let say r from the elements of Array instance. If you are providing a number 'n' along with the method, then the method will return an Array instance which will be of length 'n' and contains unrepeated and unique random numbers. If the method is invoked without providing any parameter, the method will return 'nil' if the Array instance is found to be empty and an empty Array object is returned if the method is invoked with the argument with the Array which has no elements.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是,从Array实例的元素中选择一个随机对象,例如r。 如果与方法一起提供数字“ n” ,则该方法将返回一个数组实例,该实例的长度为“ n”,并且包含未重复且唯一的随机数。 如果在没有提供任何参数的情况下调用该方法,则如果发现Array实例为空,则该方法将返回“ nil”;如果使用带有不带任何元素的Array的参数调用该方法,则该方法将返回空Array对象。

Syntax:

句法:

    array_instance.sample -> object
or
array_instance.sample(n)-> new_array

Argument(s) required:

所需参数:

This method takes one argument which decides the length of the Array instance which is returned by the method.

此方法采用一个参数,该参数确定该方法返回的Array实例的长度。

Example 1:

范例1:

=begin
Ruby program to demonstrate sample method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array sample implementation"
rn = table.sample
puts "The random object generated from Array instance is #{rn}"

Output

输出量

RUN 1:
Array sample implementation
The random object generated from Array instance is 8
RUN 2:
Array sample implementation
The random object generated from Array instance is 18

Explanation:

说明:

In the above code, you can observe that we are generating random numbers from the Array instance with the help of Array.sample() method. In both the Runs, you can observe that the random numbers being generated are totally different from each other.

在上面的代码中,您可以观察到借助于Array.sample()方法 ,我们正在从Array实例生成随机数。 在两次运行中,您都可以观察到生成的随机数彼此完全不同。

Example 2:

范例2:

=begin
Ruby program to demonstrate sample method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array sample implementation"
puts "Enter the number of objects you want to generate:"
num = gets.chomp.to_i
rn = table.sample(num)
puts "The random objects generated from Array instance is #{rn}"

Output

输出量

RUN 1:
Array sample implementation
Enter the number of objects you want to generate:
3
The random objects generated from Array instance is [20, 18, 2]
RUN 2:
Array sample implementation
Enter the number of objects you want to generate:
3
The random objects generated from Array instance is [2, 6, 8]

Explanation:

说明:

In the above code, you can observe that we are generating an Array instance of Random numbers from the Array instance with which Array.sample() method has been invoked. In both the Runs, you can see that the Array instances obtained are totally different from each other.

在上面的代码中,您可以观察到我们正在从调用Array.sample()方法的Array实例生成随机数的Array实例。 在这两次运行中,您都可以看到获得的Array实例彼此完全不同。

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

ruby array

ruby array_Ruby中带有示例的Array.sample()方法相关推荐

  1. ruby array_Ruby中带有示例的Array.shuffle方法

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

  2. ruby array_Ruby中带有示例的Array.select方法

    ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...

  3. ruby array_Ruby中带有示例的Array.keep_if方法

    ruby array Ruby Array.keep_if方法 (Ruby Array.keep_if Method) In the last articles, we have studied th ...

  4. ruby array_Ruby中带有示例的Array.delete_if方法

    ruby array Ruby Array.delete_if方法 (Ruby Array.delete_if Method) In the last articles, we have studie ...

  5. ruby array_Ruby中带有示例的Array.cycle()方法

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  6. ruby array_Ruby中带有示例的Array.fill()方法(1)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  7. ruby array_Ruby中带有示例的Array.index()方法

    ruby array Array.index()方法 (Array.index() Method) In this article, we will study about Array.index() ...

  8. ruby array_Ruby中带有示例的Array.fill()方法(3)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  9. ruby array_Ruby中带有示例的Array.zip()方法

    ruby array Array.zip()方法 (Array.zip() Method) In this article, we will study about Array.zip() Metho ...

最新文章

  1. 报错内容 IndentationError: unindent does not match any outer indentation level
  2. NEC使用C+L EDFA在超过1.1万公里的海底光缆中首次实现50Tb传输
  3. PHP微信SDK——Zebra-Wechat
  4. ssm提交post_我用spring mvc做,用post方式提交,后台获取不到参数值,用get方式就可以,什么问题...
  5. laravel5.5 php7,ubuntu 16.04+nginx+mysql+php7.1+laravel5.5环境
  6. DataList多行数据后如何添加一条分隔线
  7. 月球好忙 蓝色起源2024年要将首位女性送上月球表面
  8. Flex与.NET互操作(十六):FluorineFx + Flex视频聊天室案例开发
  9. VC 写 TXT 文件分割器 附代码
  10. shiro 单点登录_Shiro权限管理框架(一):Shiro的基本使用
  11. lambda函数if_Python中lambda的使用,与它的三个好基友介绍
  12. Window Operations
  13. (二)关于ThinkPHP2.1版本操作MSSQL类的BUG--selec查询只得出1条记录
  14. [AngularJS] 插件ui-grid使用说明
  15. mapgis矢量化怎么打分数_MapGIS67操作手册(3-12)MapGIS67矢量化的基本流程
  16. 系統封裝(4)--封裝系統(龍帝國封裝工具)
  17. 从高德地图获取城市地铁线路+站点
  18. Evernote推出实体笔记本Evernote Business Notebook,支持搜索手写笔记,用实体便签将笔记同步到Evernote上的相应分类
  19. 数字滤波算法——程序判断滤波
  20. unity使用videoplayer播放视频黑屏问题解决方案

热门文章

  1. python实现队列_用Python实现的数据结构与算法:队列
  2. 使用系统调用pipe建立一条管道线_使用Unixbench对服务器综合性能打分及测试结果...
  3. epson彩色打印机加墨水_爱普生打印机墨盒如何加墨?
  4. LDAP命令介绍---ldappasswordmodify口令修改操作
  5. MIP 扩展组件开发手册
  6. 【数学】Hunter’s Apprentice
  7. 数据中心部署气流遏制系统需要考虑的十大要素
  8. Launch Instruments
  9. spark源码分析之Executor启动与任务提交篇
  10. 数据结构学习笔记(六)链表算法题