Problem with if statement

local number = math.random() local function adChance(event) if number \>= 0.1 then appodeal.show( "rewardedVideo" ) print(number) elseif number \>= 0.2 then print("no ad") print(number) elseif number \>= 0.3 then print("no ad") print(number) elseif number \>= 0.4 then print("no ad") print(number) elseif number \>= 0.5 then print("no ad") print(number) elseif number \>= 0.6 then print("no ad") print(number) elseif number \>= 0.7 then print("no ad") print(number) elseif number \>= 0.8 then print("no ad") print(number) elseif number \>= 0.9 then print("no ad") print(number) elseif number \>= 0.10 then print("no ad") print(number) end end adChance()

I tried to make an if statement where if the variable number (which equals math.random) equals to 0.1 or less, an ad will play(I use appodeal). If anything else, “no ad” will be printed on the console. However, an ad will play everytime the scene is loaded. I am pretty sure that the if statement is wrong since the chances of the ad to appear is 10%. Any idea how the statement should look like?

I just realized how to do it

here it is:

local number = math.random() local function adChance(event) if number \<= 0.3 then appodeal.show( "rewardedVideo" ) print(number) elseif number \> 0.3 then print("no ad") print(number) end end adChance()
local number=math.random() if number \<0.3 then -- show your ad else -- show other end

no need for a second if statement

I’d recommend using anaqim’s solution within your adChance function. Currently, you’ll either always get a video or never get a video as you never update ‘number’.

or to save a few CPU cycles

if&nbsp;math.random()&nbsp;\<&nbsp;0.3 then -- show your ad else -- show other end

yeah what sphere said, even better

I know you already marked this as solved, but you do realize that you made those if-then-else checks in the wrong order right?

If statements are evaluated in order so all of the checks for values > 0.1 are a waste of time.  because the first check will always fire.

-- ANY VALUE \>= 0.1 and this will fire -- if number \>= 0.1 then appodeal.show( "rewardedVideo" ) print(number) -- THIS AND ALL FOLLOWING CHECKS WILL NEVER FIRE!!! -- elseif number \>= 0.2 then print("no ad")

I just realized how to do it

here it is:

local number = math.random() local function adChance(event) if number \<= 0.3 then appodeal.show( "rewardedVideo" ) print(number) elseif number \> 0.3 then print("no ad") print(number) end end adChance()
local number=math.random() if number \<0.3 then -- show your ad else -- show other end

no need for a second if statement

I’d recommend using anaqim’s solution within your adChance function. Currently, you’ll either always get a video or never get a video as you never update ‘number’.

or to save a few CPU cycles

if&nbsp;math.random()&nbsp;\<&nbsp;0.3 then -- show your ad else -- show other end

yeah what sphere said, even better

I know you already marked this as solved, but you do realize that you made those if-then-else checks in the wrong order right?

If statements are evaluated in order so all of the checks for values > 0.1 are a waste of time.  because the first check will always fire.

-- ANY VALUE \>= 0.1 and this will fire -- if number \>= 0.1 then appodeal.show( "rewardedVideo" ) print(number) -- THIS AND ALL FOLLOWING CHECKS WILL NEVER FIRE!!! -- elseif number \>= 0.2 then print("no ad")