Jump to content

Random Number Generator


Recommended Posts

There's 2 functions for this. An integer and a float.

 

(Examples only)

 

GlobalVariable property myGlobal auto
 
Function Something()
    Int iRand  = Utility.RandomInt(0, 50)
    myGlobal.SetValue(iRand as Float)
EndFunction
 
Function Something2()
    Float fRand = Utility.RandomFloat(0.0, 50.0)
    myGlobal.SetValue(fRand)
EndFunction
Link to comment
Share on other sites

  • 1 month later...

This script always gives me 0

Help please.

 

 

ObjectReference Property ChairMarkerREF Auto
Actor Property PlayerRef Auto
Sound Property MySND0 Auto
Sound Property MySND1 Auto
GlobalVariable Property RndNum Auto

Function Something()
Int iRand = Utility.RandomInt(0, 2)
RndNum.SetValue(iRand as Int)
EndFunction

Event OnActivate(ObjectReference akActionRef)
if akActionRef == PlayerREF
ChairMarkerREF.Activate(PlayerREF)
Utility.wait(4.0)
If RndNum.GetValue() == 0
Debug.Notification ("Random 0 returned")
MySND0.play(self)
Endif
If RndNum.GetValue() == 1
Debug.Notification ("Random 1 returned")
Game.DisablePlayerControls()
MySND1.play(self)
Utility.wait(4.0)
Game.EnablePlayerControls()
Endif
If RndNum.GetValue() == 2
Debug.Notification ("Random 2 returned")

Endif
endif

EndEvent

 

 

Changed...

RndNum.SetValue(iRand as int)

to

RndNum.SetValue(iRand as float)

 

Still 0

Edited by antstubell
Link to comment
Share on other sites

Script as written has nothing calling the Something function. As a result, there is no random value generated or applied to the global variable. Thus the returned value of the global variable will always be the initial value that you gave the record. Which in this case seems to have been 0.

 

Call the function inside the OnActivate event prior to checking for the random value.

 

 

Function Something()
Int iRand = Utility.RandomInt(0, 2)
RndNum.SetValue(iRand as Int)
EndFunction

Event OnActivate(ObjectReference akActionRef)
if akActionRef == PlayerREF
Something()
ChairMarkerREF.Activate(PlayerREF)
Utility.wait(4.0)
If RndNum.GetValue() == 0
Debug.Notification ("Random 0 returned")
MySND0.play(self)
Endif
If RndNum.GetValue() == 1
Debug.Notification ("Random 1 returned")
Game.DisablePlayerControls()
MySND1.play(self)
Utility.wait(4.0)
Game.EnablePlayerControls()
Endif
If RndNum.GetValue() == 2
Debug.Notification ("Random 2 returned")

Endif
endif

EndEvent

 

 

Link to comment
Share on other sites

Something() function is not called. You wont need that though, you can put the code for that right in the Event you're using. Then it will start generating a random number on every activation.

 

Edit: ninja'd lol

Edited by Rasikko
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...