红宝石 - 插入数组基于百分比 [英] Ruby - insert into an array based on a percentage

查看:133
本文介绍了红宝石 - 插入数组基于百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 TestSubject ,我可以用它来访问以下数据的对象,名称百分比我想根据百分比来插入名字,到一个数组。因此,例如,

arr.push(TestSubject.name)基于TestSubject的百分比,在这种情况下,它的提出TestSubject.percent#=> 90

这意味着这个特定的名称有被推到阵列的90%的机会。我并不怎么过知道如何写在Ruby中的语法。

任何想法?


解决方案

  arr.push(TestSubject.name)如​​果兰特(100)LT = TestSubject.percent

I have an object called TestSubject which I can use to access the following data, name and percent I want to insert the name based on the percent, into an array. so for example,

arr.push(TestSubject.name) based on the percentage of TestSubject, in this case its puts TestSubject.percent #=> 90

This means that this specific name has a 90% chance of being pushed to the array. How ever I am not sure how to write that in ruby syntax.

Any ideas?

解决方案

arr.push(TestSubject.name) if rand(100) <= TestSubject.percent

这篇关于红宝石 - 插入数组基于百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆