View unanswered posts | View active topics It is currently Fri Dec 27, 2024 7:03 am



Reply to topic  [ 8 posts ] 
 Surprise Surprise 
Author Message
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1360
Location: USA
Reply with quote
Post 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
Profile
User avatar

Joined: Sun May 11, 2008 12:50 pm
Posts: 899
Reply with quote
Post 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
Profile WWW
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1360
Location: USA
Reply with quote
Post 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
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post Re: Surprise Surprise
Code:
timer:Reset()


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
Profile

Joined: Mon Jun 08, 2009 10:54 pm
Posts: 33
Reply with quote
Post 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
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Surprise Surprise
http://lua-users.org/wiki/MathLibraryTutorial

Quote:
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
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post 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
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post 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
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.028s | 14 Queries | GZIP : Off ]