Jump to content




How do I add a small chance of something happening?

computer game lua

  • You cannot reply to this topic
26 replies to this topic

#1 Kadecamz

  • New Members
  • 113 posts

Posted 15 August 2012 - 07:38 PM

I have a missile launching script.
But I want to make there be a small chance that it will fail (15%)
How can I do this?

This is the code, just incase you want it to see what you can do.

local code = "1337"
term.clear()
sleep(0.1)
print "Enter the code! Be sure the silo is powered."
write "CODE: "
input = read()
if input == "1337" then
write "Accessing mainframe"
sleep (2)
write "."
sleep (2)
write "."
sleep(1)
write "."
sleep (1)
term.clear()
sleep(0.1)
term.setCursorPos(1,1)
print "Success! The missiles are firing in"
write "3"
sleep(1)
term.clearLine()
term.setCursorPos(1,2)
write "2"
sleep(1)
term.clearLine()
term.setCursorPos(1,2)
write "1"
sleep(1)
print ".....FIRING!!!!"
sleep(0.3)
rs.setOutput ("right", true)
print "Action complete"
sleep(2)
os.shutdown()
elseif input == "modify" then
print "You may now modify the mainframe"
sleep(1)
elseif input == "abort" then
write "Aborting Files"
sleep(1)
write "."
sleep(1)
write "."
sleep(1)
write "."
sleep(0.5)
term.setCursorPos(1,5)
print "Successfully Aborted!"
sleep(3)
os.shutdown()
else
write "Accessing mainframe"
sleep(1)
write "."
sleep(1)
write "."
sleep(1)
write "."
term.clearLine()
term.setCursorPos(1,4)
print "Wrong code!"
sleep(3)
os.shutdown()
end

Hope you can help. :(/>

#2 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 15 August 2012 - 07:42 PM

chance = math.random(1, 5)
if chance == 1 then
  --Succsess code
elseif chance == 2 then
  --sucsess code
elseif chance == 3 then
  --sucsess code
elseif chance == 4 then
  --sucsess code
elseif chance == 5 then
  --fail code
end

This would give a 20% chance of failiure

#3 Kadecamz

  • New Members
  • 113 posts

Posted 15 August 2012 - 07:44 PM

Thankyou!

#4 Kadecamz

  • New Members
  • 113 posts

Posted 15 August 2012 - 07:50 PM

errrrrrr. How would I get it to do that but with the success message all doing the same thing?

EDIT: I got it, but now I'm gonna do some pretty large code with it.
Any lua coding programs?
I mean like..not notepad.
Actual lua coding textpads.

#5 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 15 August 2012 - 07:54 PM

Make sucsess scrip a function?
function sucsess()
  --Code for sucsess
end

And for fail code

function fail()
  -- Code for fail
end

And then do something like this
chance = math.random (1, 5)

if chance == 1 then
  sucsess()
elseif chance == 2 then
  sucsess()
elseif chance == 3 then
  sucsess()
elseif chance == 4 then
  sucsess()
elseif chance == 5 then
  fail()
end


#6 Kadecamz

  • New Members
  • 113 posts

Posted 15 August 2012 - 08:13 PM

k.
got the bare code out now. ;p

EDIT: By that I mean I undid all my long stupidness

#7 D3matt

  • Members
  • 830 posts

Posted 15 August 2012 - 08:14 PM

Much simpler:

chance = math.random (1, 5)

if chance == 1 then
  fail()
else
  success()
end
Either that or use less than/greater than operators.

#8 Kadecamz

  • New Members
  • 113 posts

Posted 15 August 2012 - 08:23 PM

Mn...I'mma go with SJ's idea.
But I'll use that one if I do this again in the future. :(/>

Here is the code so far

local code = "1337"
term.clear()
sleep(0.1)
term.setCursorPos(1,1)
print "Enter the code! Be sure the silo is powered."
write "CODE: "
input = read()
if input == "1337" then
function success()
write "Accessing mainframe"
sleep (2)
write "."
sleep (2)
write "."
sleep(1)
write "."
sleep (1)
term.clear()
sleep(0.1)
term.setCursorPos(1,1)
print "Success! The missiles are firing in"
write "3"
sleep(1)
term.clearLine()
term.setCursorPos(1,2)
write "2"
sleep(1)
term.clearLine()
term.setCursorPos(1,2)
write "1"
sleep(1)
print ".....FIRING!!!!"
sleep(0.3)
rs.setOutput ("right", true)
print "Action complete"
sleep(2)
os.shutdown()
end
function fail()
term.clear()
term.setCursorPos(1,1)
sleep(0.1)
write "Failure has accoured."
sleep(3)
os.shutdown()
end
chance = math.random (1,5)
if chance == 1 then
success()
elseif chance == 2 then
success()
elseif chance == 3 then
success()
elseif chance == 4 then
success()
elseif chance == 5 then
fail()
end
elseif input == "modify" then
print "You may now modify the mainframe"
sleep(1)
elseif input == "abort" then
write "Aborting Files"
sleep(1)
write "."
sleep(1)
write "."
sleep(1)
write "."
sleep(0.5)
term.setCursorPos(1,5)
print "Successfully Aborted!"
sleep(3)
os.shutdown()
else
write "Accessing mainframe"
sleep(1)
write "."
sleep(1)
write "."
sleep(1)
write "."
term.clearLine()
term.setCursorPos(1,4)
print "Wrong code!"
sleep(3)
os.shutdown()
end

Edit: Still working with the setCursorPos's so that it will work smoothly and look nicer.

#9 Ponder

  • New Members
  • 49 posts

Posted 15 August 2012 - 10:38 PM

You should really go with D3matt's suggestion, it's much cleaner and avoids redundancy, which is what you want as a programer.

#10 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 16 August 2012 - 12:01 AM

View PostD3matt, on 15 August 2012 - 08:14 PM, said:

Much simpler:

chance = math.random (1, 5)

if chance == 1 then
  fail()
else
  success()
end
Either that or use less than/greater than operators.

View PostPonder, on 15 August 2012 - 10:38 PM, said:

You should really go with D3matt's suggestion, it's much cleaner and avoids redundancy, which is what you want as a programer.

Yeah. D3Matt's post means you can change the percentage very easily, too. 20% too high a chance for failiure? Make it 5%:
chance = math.random(1, 20)
if chance == 1 then
fail()
else
success()
end
I changed 1 number. You have to write more than 40 lines of code, even using functions, with sjele's method. His method works, but is redundant.

#11 Kadecamz

  • New Members
  • 113 posts

Posted 16 August 2012 - 12:14 AM

I do all single player coding with notepad++
So it was just a simple copy and paste.

#12 cant_delete_account

  • Members
  • 484 posts

Posted 16 August 2012 - 12:39 AM

View PostKadecamz, on 16 August 2012 - 12:14 AM, said:

I do all single player coding with notepad++
So it was just a simple copy and paste.
Still, it's running more if statements than needed. And is way longer than needed.

#13 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 16 August 2012 - 08:42 AM

And if you want to change the percentage absolutely precisely, do:
chance = 15 --percentage of chance
if math.random(1,100) > chance then -- if random number is more than chance, in this case 15
success()
else
fail()
end

I also feel the need to point out that instead of doing all those sleeps, for the shorter sleeps you could use textutils.slowWrite/textutils.slowPrint eg:
textutils.slowWrite("...")
as opposed to your current
write(".")
sleep(1)
write(".")
sleep(1)
write(".")
sleep(1)
Or perhaps even doing
write(".") sleep(1) write(".") sleep(1) write(".") sleep(1)
to save space and make your code more readable.

Ps: I would like to point out that you horribly misspelt occurred.

#14 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 August 2012 - 08:48 AM

shouldn't it be math.random(1,100)?

#15 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 16 August 2012 - 08:48 AM

View PostKaoS, on 16 August 2012 - 08:48 AM, said:

shouldn't it be math.random(1,100)?
What percentage did he ask for in the first place?

#16 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 August 2012 - 08:50 AM

not sure, but it doesn't matter, for a percentage chance you do
if percentchance>=math.random(1,100) then
--successcode
end


#17 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 16 August 2012 - 08:56 AM

View PostKaoS, on 16 August 2012 - 08:50 AM, said:

not sure, but it doesn't matter, for a percentage chance you do
if percentchance>=math.random(1,100) then
--successcode
end
Then say that in the first place D:

--Code edited accordingly--

#18 D3matt

  • Members
  • 830 posts

Posted 16 August 2012 - 09:20 AM

View PostKaoS, on 16 August 2012 - 08:50 AM, said:

not sure, but it doesn't matter, for a percentage chance you do
if percentchance>=math.random(1,100) then
--successcode
end
That was my other suggestion but I couldn't remember the way less/greater operators were written in LUA.

#19 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 August 2012 - 09:20 AM

View PostPharap, on 16 August 2012 - 08:42 AM, said:

And if you want to change the percentage absolutely precisely, do:
chance = 15 --percentage of chance
if math.random(1,100) > chance then -- if random number is more than chance, in this case 15
success()
else
fail()
end

I also feel the need to point out that instead of doing all those sleeps, for the shorter sleeps you could use textutils.slowWrite/textutils.slowPrint eg:
textutils.slowWrite("...")
as opposed to your current
write(".")
sleep(1)
write(".")
sleep(1)
write(".")
sleep(1)
Or perhaps even doing
write(".") sleep(1) write(".") sleep(1) write(".") sleep(1)
to save space and make your code more readable.

Ps: I would like to point out that you horribly misspelt occurred.

the chance must be greater or equal to the random math function in order for it to work, just need to flip that '>' and perhaps add an '=' or you will always be 1% short which isn't much I suppose

#20 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 16 August 2012 - 10:24 AM

View PostKaoS, on 16 August 2012 - 09:20 AM, said:

View PostPharap, on 16 August 2012 - 08:42 AM, said:

And if you want to change the percentage absolutely precisely, do:
chance = 15 --percentage of chance
if math.random(1,100) > chance then -- if random number is more than chance, in this case 15
success()
else
fail()
end

I also feel the need to point out that instead of doing all those sleeps, for the shorter sleeps you could use textutils.slowWrite/textutils.slowPrint eg:
textutils.slowWrite("...")
as opposed to your current
write(".")
sleep(1)
write(".")
sleep(1)
write(".")
sleep(1)
Or perhaps even doing
write(".") sleep(1) write(".") sleep(1) write(".") sleep(1)
to save space and make your code more readable.

Ps: I would like to point out that you horribly misspelt occurred.

the chance must be greater or equal to the random math function in order for it to work, just need to flip that '>' and perhaps add an '=' or you will always be 1% short which isn't much I suppose
Are you sure that's right?
If I do it the way it is now, if random (eg 86) is larger than 15 (not equal to) then success(), but if it's not (ie less than or equal to) eg 13, then fail() is called.
15 doesn't count as larger than 15, so numbers 1-15 are handled by the else condition.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users