Code
  • 首页
  • cpu-architecture
  • performance
  • c++
  • branch-prediction
  • java
2023-05-11 10:00:01

如何在Ruby中获取随机数

randomrubyruby-on-railsruby-on-rails-3ruby-on-rails-4

如何生成介于0和n之间的随机数?


当前回答

这个怎么样?

num = Random.new
num.rand(1..n)
2016-03-11 14:31:35

其他回答

你可以做rand(范围)

x = rand(1..5)
2015-08-22 23:09:00

问题的最简单答案:

rand(0..n)
2015-02-19 18:53:15

在ruby中获取随机数的简单方法是,

def random    
  (1..10).to_a.sample.to_s
end
2017-02-16 10:04:47

别忘了先用srand()为RNG种子。

2008-10-13 18:15:52

这个怎么样?

num = Random.new
num.rand(1..n)
2016-03-11 14:31:35

推荐文章

  • 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
  • 是否可以在MiniTest中运行单个测试?
  • 如何在Ruby中生成a和b之间的随机数?
  • ActiveRecord OR查询
  • 无法安装gem -未能建立gem本地扩展-无法加载这样的文件——mkmf (LoadError)
  • 如何在Ruby中创建文件
  • 什么是Ruby文件。开放模式和选项?
  • Ruby数组到字符串的转换
  • 如何分割(块)一个Ruby数组成X元素的部分?
  • Ruby中“or”和||的区别?
  • java.util.Random真的那么随机吗?我怎么能生成52!(阶乘)可能的序列?
  • 从数组中随机选择一个元素
  • 在Rails中使用user_id:integer vs user:references生成模型
  • 如何测试参数是否存在在轨道
  • 验证多个列的唯一性

aliyun

最新文章

  • 如何删除默认的导航栏空间在SwiftUI导航视图
  • 我可以列出所有的标准Go包吗?
  • 证书验证失败:无法获得本地颁发者证书
  • 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
  • 打开同一目录两次
  • 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
  • NullInjectorError: AngularFirestore没有提供程序
  • 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
  • 从元组/数组值派生联合类型
  • 如何在Angular2中截断文本?
  • 安装tzdata非交互式
  • ionic 2 - Error在Android Studio中找不到Gradle的安装版本
  • 如何查看所有地区所有正在运行的Amazon EC2实例?
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 安装APK时出现错误

标签

cpu-architectureperformancec++branch-predictionjavaundogit-commitgitversion-controlgit-remotegit-pushgit-branchgit-pullgit-fetchpythongeneratoriteratorjsonmime-typescontent-typearraysjavascriptgit-addcode-formattingcstandards-complianceoperatorscommentsdynamic-memory-allocationstackmemory-managementheap-memorylanguage-agnosticoverwritebackground-colorbrowserhtmldomjqueryvisibilityuse-strictsyntaxjslintremote-branchgit-checkoutpython-moduleidiomsprogram-entry-pointnamespacesbranch
2025 code 京ICP备15047053号-1