[Rails] I need to run some code only 40% of the times, how may i do this?
i nee开发者_如何学Cd to make some square items, and there can be a max of 5 per row, and a min of 1 per row. then i want to have a row with 2 boxes, 5boxe, 1 box and so on, random... the last box of the row, will have a clear:both class..
Generate a random number between 0.0 and 1.0 for instance and check if it's less than or equal to 0.4.
If you need to ensure that your task is run 4 out of 10 times then use this method:
some_task if run?
def run?()
@i=(@i||0) + 1
(@i-1)%10 < 4
end
Caveat:
You need to run the task at least 10 times to get the 40% distribution.
Distribution is not random
精彩评论