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

#21 BigSHinyToys

  • Members
  • 1,001 posts

Posted 16 August 2012 - 10:33 AM

View Postsjele, on 15 August 2012 - 07:54 PM, said:

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


#22 KaoS

    Diabolical Coder

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

Posted 16 August 2012 - 10:33 AM

yes but we are looking for a 15% chance of success here

unless I misunderstood OP

#23 KaoS

    Diabolical Coder

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

Posted 16 August 2012 - 10:39 AM

lol, looks like I was wrong, apologies Pharap, misread

#24 BigSHinyToys

  • Members
  • 1,001 posts

Posted 16 August 2012 - 10:40 AM

View PostKaoS, on 16 August 2012 - 10:33 AM, said:

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


#25 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 16 August 2012 - 10:51 AM

View PostKaoS, on 16 August 2012 - 10:39 AM, said:

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.

View PostBigSHinyToys, on 16 August 2012 - 10:40 AM, said:

View PostKaoS, on 16 August 2012 - 10:33 AM, said:

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.

#26 KaoS

    Diabolical Coder

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

Posted 16 August 2012 - 10: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

#27 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 16 August 2012 - 10:56 AM

View PostKaoS, on 16 August 2012 - 10:55 AM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users