113 posts
Posted 15 August 2012 - 08: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. :(/>/>
318 posts
Location
Somewhere on the planet called earth
Posted 15 August 2012 - 08: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
113 posts
Posted 15 August 2012 - 08:44 PM
Thankyou!
113 posts
Posted 15 August 2012 - 08: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.
318 posts
Location
Somewhere on the planet called earth
Posted 15 August 2012 - 08: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
113 posts
Posted 15 August 2012 - 09:13 PM
k.
got the bare code out now. ;p
EDIT: By that I mean I undid all my long stupidness
686 posts
Posted 15 August 2012 - 09: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.
113 posts
Posted 15 August 2012 - 09: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.
49 posts
Posted 15 August 2012 - 11: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.
209 posts
Location
In your fridge.
Posted 16 August 2012 - 01:01 AM
Much simpler:
chance = math.random (1, 5)
if chance == 1 then
fail()
else
success()
end
Either that or use less than/greater than operators.
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.
113 posts
Posted 16 August 2012 - 01:14 AM
I do all single player coding with notepad++
So it was just a simple copy and paste.
474 posts
Posted 16 August 2012 - 01:39 AM
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.
839 posts
Location
England
Posted 16 August 2012 - 09: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.
1548 posts
Location
That dark shadow under your bed...
Posted 16 August 2012 - 09:48 AM
shouldn't it be math.random(1,100)?
839 posts
Location
England
Posted 16 August 2012 - 09:48 AM
shouldn't it be math.random(1,100)?
What percentage did he ask for in the first place?
1548 posts
Location
That dark shadow under your bed...
Posted 16 August 2012 - 09:50 AM
not sure, but it doesn't matter, for a percentage chance you do
if percentchance>=math.random(1,100) then
--successcode
end
839 posts
Location
England
Posted 16 August 2012 - 09:56 AM
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–
686 posts
Posted 16 August 2012 - 10:20 AM
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.
1548 posts
Location
That dark shadow under your bed...
Posted 16 August 2012 - 10:20 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.
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
839 posts
Location
England
Posted 16 August 2012 - 11:24 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.
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.
992 posts
Posted 16 August 2012 - 11:33 AM
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
Improved your script a little
chance = math.random (1, 500) -- much longer shot
if chance == 1 then
sucsess()
else
fail()
end
1548 posts
Location
That dark shadow under your bed...
Posted 16 August 2012 - 11:33 AM
yes but we are looking for a 15% chance of success here
unless I misunderstood OP
1548 posts
Location
That dark shadow under your bed...
Posted 16 August 2012 - 11:39 AM
lol, looks like I was wrong, apologies Pharap, misread
992 posts
Posted 16 August 2012 - 11:40 AM
yes but we are looking for a 15% chance of success here
unless I misunderstood OP
Ok here
chance = math.random (1, 100) -- much longer shot
if chance <=15 then
fail()
else
sucsess()
end
839 posts
Location
England
Posted 16 August 2012 - 11:51 AM
lol, looks like I was wrong, apologies Pharap, misread
I had the feeling you did since I swapped the order of the test.
Programmers have to be on their toes, you know.
yes but we are looking for a 15% chance of success here
unless I misunderstood OP
Ok here
chance = math.random (1, 100) -- much longer shot
if chance <=15 then
fail()
else
sucsess()
end
We've passed that and established
if math.random (1, 100) <=15 then
fail()
else
sucsess()
end
is shorter, but if you want to be able to set chance to the percentage chance, use
chance = 15
if math.random (1, 100) <= chance then
fail()
else
sucsess()
end
After this, if anybody ever asks how to manage random chance occurrences, I'm going to throw my computer out the window.
1548 posts
Location
That dark shadow under your bed...
Posted 16 August 2012 - 11:55 AM
hahahahaha, I dealt with this forever in DOS, its random function/re-evaluating var is harder to manipulate especially as you have to be VERY careful with maths in dos as at every step it rounds off to the nearest integer :(/>/> what a nightmare
839 posts
Location
England
Posted 16 August 2012 - 11:56 AM
hahahahaha, I dealt with this forever in DOS, its random function/re-evaluating var is harder to manipulate especially as you have to be VERY careful with maths in dos as at every step it rounds off to the nearest integer :(/>/> what a nightmare
I would like to take this moment to thank programming for the modulo feature, and pray that someday it comes as standard on calculators.