• You've discovered RedGuides, an EverQuest multi-boxing and scripting community 🧙‍♀️⚙️. We want you to play several EQ characters at once, come join us and say hello! 👋

  • A TLP without truebox has thawed (Very Vanilla ready)
    Frostreaver

Problem - Problems with Math.Rand

Joined
Mar 20, 2024
RedCents
1,051¢
I am trying to implement a randomizer so the script randomly picks one of the group members to cast a particular spell.

I got the following code:

Code:
local RandomIndex = mq.TLO.Math.Rand(1, 6) --- Generate a random number from 1 to 6
local RandomMember = "Character1" --- This is just a default to declare the varible

if RandomIndex == 1 then
    RandomMember = "Character1"
elseif RandomIndex == 2 then
    RandomMember = "Character2"
elseif RandomIndex == 3 then
    RandomMember = "Character3"
elseif RandomIndex == 4 then
    RandomMember = "Character4"
elseif RandomIndex == 5 then
    RandomMember = "Character5"
elseif RandomIndex == 6 then
    RandomMember = "Character6"     
end

print(RandomIndex)
print(RandomMember)

The problem I am having is that print(RandomIndex) does indeed print a random number from 1 to 6, but print(RandomMember) always prints the default "Character1". That means that the If / Elseif rules are ignored altogether. I cannot figure out where my syntax is wrong.

Any insights please? Thanks!
 
oh no dont use TLO.Math in a Lua, use the Lua math library

you gonna much happier

 
The core issue is is that you are comparing a string to a number, and Lua is strongly typed so that comparison will always fail. First, do what kaen says to get an actual number, but also remember to always close your datatypes with () to get an actual usable Lua type.
 
don't forget to properly seed! (if you run this at the same time on different chars, that is)
 
I am trying to implement a randomizer so the script randomly picks one of the group members to cast a particular spell.

I got the following code:

Code:
local RandomIndex = mq.TLO.Math.Rand(1, 6) --- Generate a random number from 1 to 6
local RandomMember = "Character1" --- This is just a default to declare the varible

if RandomIndex == 1 then
    RandomMember = "Character1"
elseif RandomIndex == 2 then
    RandomMember = "Character2"
elseif RandomIndex == 3 then
    RandomMember = "Character3"
elseif RandomIndex == 4 then
    RandomMember = "Character4"
elseif RandomIndex == 5 then
    RandomMember = "Character5"
elseif RandomIndex == 6 then
    RandomMember = "Character6"     
end

print(RandomIndex)
print(RandomMember)

The problem I am having is that print(RandomIndex) does indeed print a random number from 1 to 6, but print(RandomMember) always prints the default "Character1". That means that the If / Elseif rules are ignored altogether. I cannot figure out where my syntax is wrong.

Any insights please? Thanks!

Since Math.Rand is a TLO you need terminate it with a () to make it a number

edit: lol everybody replied at exactly the same time :D
 
for your character name, just do
Lua:
mq.TLO.Group.Member(RandomIndex - 1).CleanName()  -- -1 because group is from 0 to 5

that gets you that person in group
 
math.randomseed (x)

Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.

This is how I do it most of the time:

math.randomseed(os.time()*mq.TLO.Me.ID())
 
Or hotkey using DanNet.
Code:
 /dgga /docommand /timed $\{Math.Calc[$\{Math.Rand[590]} +10].Int} /casting "Gate"
 
math.randomseed (x)

Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.

This is how I do it most of the time:

math.randomseed(os.time()*mq.TLO.Me.ID())
This. The benefit of using the time from your os is that it is always changing. If you don't seed (or seed with the same number all the time), then your 'random' sequence will always be the same every time you start it.
 
Thanks everybody for the suggestions, very kind. I think I got the right code based on the responses in this thread, see code below:

Code:
math.randomseed(os.clock())
local RandomIndex = mq.TLO.Math.Rand(1, 6)()
local RandomMember = mq.TLO.Group.Member(RandomIndex - 1).CleanName()  --- This gives me the name of the character
print(RandomMember)

This snippet of code is embedded within a larger code that I must run for all characters. The new problem I am observing is that each character is printing a different RandomMember, whereas I would need them all to be "in sync", meaning that every time the script runs in each of the clients (characters), the same RandomMember should be printed for all.

How do you sync up a Math.Rand across all clients within the dannet?
 
Anyone? is it even possible?
if you provide the same seed, the same results will occur :) but only if you do the same random calls on each toon.

But you need to describe what exactly you want to accomplish...

"I need all of my 6 toons to target the same random group member" probably best solved by doing the random on one, then telling the other members whom to target
 
Problem - Problems with Math.Rand

Users who are viewing this thread

Back
Top
Cart