A: The simplest random (keeping in mind that no programming language or computer can ever truly be random) is to use a timestamp for a seed. Here’s an example of a client-side Lua script for RedM/CFX.
-- create a random seed using the game timmer
local random_seed_ = GetGameTimer()
-- display if your curious
print("GetGameTimer:", random_seed_)
-- generate the random seed that the "math.random" uses
math.randomseed(random_seed_)
-- generate your random in a number range.
-- minimum set to 1, and max set to 10
random_int = math.random( math.tointeger(1), math.tointeger(10))
Q: That’s cool and all but how do I use that practically?
A: Okay well let’s say you have an array of items and you want to randomly choose items from that array.
local random_seed_ = GetGameTimer()
print("GetGameTimer:", random_seed_)
math.randomseed(random_seed_)
-- above is same as before but here we switch
--- to pull the max of an array Config.models
random_int = math.random( math.tointeger(1), math.tointeger(#Config.models))
Q: Ok, that’s more use full but how do I really get a random selection from an array?
For example, I want a better random selection of rewards for my players.
A: Alrighty then. Here we go. You will want a multi-blind random. By multi, I mean 2 or more.
You need to have your original array of items.
Then select the random max using the above random example.
Then create a new array, and copy the items that are in 1 to your random max to the new array.
Then do those same steps again using your new array.
Do this a couple of times before you generate your random number and pull one or two items from the array to reward the player.
Each time you do this you should get a very different item array to choose from in the end.
Insert code example later.
Now in my scripts, I pull the original items from a database, based on their category. Then I can do this process on each type. Then for the final reward, I pull items from each array, so the player gets a variety of rewards each time. For example, the seed_collector script gives fruit, plants, and seeds.
Okay now we took this another step and used MIT’s epoch with GetPosixTime To get an even better seed.
-- The MIT License (MIT)
-- Copyright (c) 2018,2020 Thomas Mohaupt <thomas.mohaupt@gmail.com>
-- year: 2-digit (means 20xx) or 4-digit (>= 2000)
function datetime2epoch(second, minute, hour, day, month, year)
local mi2sec = 60
local h2sec = 60 * mi2sec
local d2sec = 24 * h2sec
-- month to second, without leap year
local m2sec = {
0,
31 * d2sec,
59 * d2sec,
90 * d2sec,
120 * d2sec,
151 * d2sec,
181 * d2sec,
212 * d2sec,
243 * d2sec,
273 * d2sec,
304 * d2sec,
334 * d2sec }
local y2sec = 365 * d2sec
local offsetSince1970 = 946684800
local yy = year < 100 and year or year - 2000
local leapCorrection = math.floor(yy/4) + 1
if (yy % 4) == 0 and month < 3 then
leapCorrection = leapCorrection - 1
end
return offsetSince1970 +
second +
minute * mi2sec +
hour * h2sec +
(day - 1) * d2sec +
m2sec[month] +
yy * y2sec +
leapCorrection * d2sec
end
Then we use this to generate our random number for our random seed.
function RandomwithNewSeed(randomMin, randomMax)
local strt_year, strt_month, strt_day, strt_hour, strt_minute, strt_second = GetPosixTime()
local _epoc = datetime2epoch(strt_second, strt_minute, strt_hour, strt_day, strt_month, strt_year)
math.randomseed(math.tointeger(_epoc))
local random_num = math.random(math.tointeger(randomMin), math.tointeger(randomMax))
return(random_num)
end
Note this only works on the client side. To get it to work on the server side your client would need to pass the epoc time variable to the server (variable). To combat potential nil issues I included an error check that defaults to pi if epoc is missing.
function SERVER_RandomwithNewSeed(randomMin, randomMax, _epoc)
if _epoc == nil then
_epoc = (3.141592653589793238*1000)
if(Config.debug== 1) then print("_epoc (nil) ",_epoc) end
else
if(Config.debug== 1) then print("_epoc ",_epoc) end
end
math.randomseed(math.tointeger(_epoc))
local random_num = math.random(math.tointeger(randomMin), math.tointeger(randomMax))
return(random_num)
end
Hits: 79