Author |
Message |
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Surprise Surprise
How do i use mamth.random to generate three different results? Like this: Code: math.random(1,3) == x if x == 1 then blah blah end if x == 2 then blah blah end if x == 3 then blah blah end end Pretty sure this doesn't work though.
|
Wed Jul 08, 2009 5:58 pm |
|
|
Roon3
Joined: Sun May 11, 2008 12:50 pm Posts: 899
|
Re: Surprise Surprise
How about: Code: x = math.floor(math.random(1,3)); if x == 1 then blah blah end if x == 2 then blah blah end if x == 3 then blah blah end end
|
Wed Jul 08, 2009 6:25 pm |
|
|
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Re: Surprise Surprise
I'll try that :3 Edit: Also, if you wanted a timer to start once something happened, how would you go at doing that? I think you aren't sposed to just do Code: if blah blah then self.timer = Timer(); end I think that's buggy/doesn't work. Like if the actor is crouched for more than 20 seconds, then blah blah.
|
Wed Jul 08, 2009 6:37 pm |
|
|
mail2345
Joined: Tue Nov 06, 2007 6:58 am Posts: 2054
|
Re: Surprise Surprise
And try: Code: if math.floor(math.random(1,3)) == 1 then blah blah end if math.floor(math.random(1,3)) == 2 then blah blah end if math.floor(math.random(1,3)) == 3 then blah blah end end
|
Wed Jul 08, 2009 7:25 pm |
|
|
TheValiant
Joined: Mon Jun 08, 2009 10:54 pm Posts: 33
|
Re: Surprise Surprise
When you use the math.random function with Lua, does it give you a different result every time, or do you have to call some sort of randomize function?
I.e. math.random = 5 math.random = 5 randomize() math.random = 3
If there is no need for a randomize function, then (@Mail) your code will return a different random number every time it is called, and you might have code from two of the if-statement-blocks run.
|
Wed Jul 08, 2009 7:47 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Surprise Surprise
http://lua-users.org/wiki/MathLibraryTutorialQuote: math.random(lower, upper) generates integer numbers between lower and upper. No math.floor needed, no randomize needed. So here's what I'd suggest: v = math.random(1,3); if v == 1 then whatever elseif v == 2 then whatever else whatever end
|
Wed Jul 08, 2009 10:20 pm |
|
|
mail2345
Joined: Tue Nov 06, 2007 6:58 am Posts: 2054
|
Re: Surprise Surprise
Ohh, it appears I mis interpeted what The Mind asked. I thought he meant how you could create 3 diffrent numbers with math.random
Grif is right.
|
Wed Jul 08, 2009 10:29 pm |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Surprise Surprise
Code: function Create(self) self.timer = Timer(); self.firsttimer = true end
function Update(self) if blah blah and self.firsttimer then self.timer:Reset() end if self.timer:IsPastSimMS(number) and not self.firsttimer then stuff end end
|
Thu Jul 09, 2009 12:50 am |
|
|
|