My weighted random method

Just quick example of something I've been using.

I have a number of optional items that I only want my regression to do sometimes, to simulate real-world examples of not filling in every field. Here is how I weight it to only perform the action 1 out of 10 times.
if random(1,10) = 1
	put "do it"
else
	put "don't do it"
end if

Of course, you can weight it however you want by changing the 10.

You could also write an external handler to return a T or F based on the passed weighting.

I hope this helps someone.

Comments

  • EggplantMattEggplantMatt ForumAdmin admin
    Thanks for posting that, Brett. A couple of notes: You don't need the range if the range starts at 1. You can just use the random function on the single number:
    put random(10)
    
    Just for "fun" you could use the merge function to write your example code block:
    put merge of "[[if random(10) is 1]]don't[[end if]] do it"
    
  • brett_xbrett_x Member
    Thanks Matt. I like the shorthand, and your idea of "fun".

    I do try to minimize my code, but try not to do so at the expense of readability to others that may have to follow what I've done. So, I think I'll take your first suggestion and leave the second one. :wink:
Sign In or Register to comment.