Savish
Savish

Reputation: 13

Ruby - insert into an array based on a percentage

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?

Upvotes: 1

Views: 706

Answers (1)

tigeravatar
tigeravatar

Reputation: 26640

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

Upvotes: 2

Related Questions