Jump to content

[Question] - How to properly randomize the turret upgrade slot


Enzo Matrix

Recommended Posts

So I thought it would be more interesting to have the turret upgrades have a randomized amount rather than a set number. I have only partially managed this but it comes out to large decimal numbers lol.

 

The current code is this.

function getNumTurrets(seed, rarity)
    return math.max(1, rarity.value + 1)
end

 

I tried this

 

function getNumTurrets(seed, rarity)
    return math.random() * (rarity.value + 1) * 1.5

 

While I had a randomized upgrade it is giving like +3.123433, +3.6346444 .. etc lol

I know I am incorrectly just slapping this code on and asking for help from the experienced ones to aid me into getting whole numbers and a range based on rarity.

 

Something like, exotic turret can have 4-7, legendary can have 5-9, and if possible make it rarer to get the higher upgrade numbers

Would be neat to make turret upgrades have a variable to them.

Link to comment
Share on other sites

math.random() provides numbers between and including 0-1. To avoid decimal places use math.floor() or math.ceil().

with your current formula you will also get a problem when math.random() returns 0.

 

I suggest using such an formula:

 

rarity.value + math.floor(math.random() * rarity.value/2)

this will provide such an distribution:

min-max

0: 0-0

1: 1-1

2: 2-3

3: 3-4

4: 4-6

5: 5-7

6: 6-9

7: 7-10

With the number before ":" refering to the numerical rarity value. I can't remember if it starts with 0 or 1. the formula might need to be fitted in that case.

Link to comment
Share on other sites

Hmm so this didn't quite work, it was getting between 5-7 on legendary however it's also constantly randomizing each time you take out the upgrade and put another one in and both upgrades will randomize again between 5-7 turrets. That's an odd one why that happens?

Link to comment
Share on other sites

assuming you placed the code in

function getNumTurrets(seed, rarity)

Then this function gets called every time you place the turretmodule.

maybe this helps:

on top of the file:

local numTurrets = nil

and

function getNumTurrets(seed, rarity)
    numTurrets = numTurrets or rarity.value + math.floor(math.random() * rarity.value/2)
    return numTurrets
end

Edit: this code is not tested and might not work properly. Test if it stays constant after restarts.

Link to comment
Share on other sites

If you want math.random() to return the same value each time for any module, you need to set the seed.

 

function getNumTurrets(seed, rarity)
    math.randomseed(seed)
    local someRand = math.random()

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...