Search code examples
imacros

Select among Three Numbers with different probability


How do i set this code(that is for 2 numbers) to select among three numbers "0.5" , "0.75" and "1" and assign them a chance to be selected for example 20% "0.5" , 50% "0.75" and 30% "1" ?

SET !VAR1 EVAL("Math.random() > 0.6 ? '1' : '0.75'")
TAG POS=1 TYPE=SELECT ATTR=ID:t_score CONTENT=%{{!VAR1}}

Solution

  • The following should work:

    Math.random() < 0.2 ? '0.5' : (Math.random() < 0.5/(0.5 + 0.3) ? '0.75' : '1')
    

    Alternatively you also could use the following logic (but I don't know how to put that into iMacros):

    var rand = Math.random();
    rand < 0.2 ? '0.5' : (rand < 0.2 + 0.5 ? '0.75' : '1')