Math.random not randomising properly

  1. What do you want to achieve? I am trying to randomise a variable to randomise the spawn point of a part.

  2. What is the issue? math.random() seems to be only working for randomising time and not other random numbers. This script ALWAYS sets randomNum1 to 1 and randomNum2 to 15.

  3. What solutions have you tried so far? I have tried to store random numbers in variables first (replaced math.random(1, randomPositionRange), 50, math.random(1, randomPositionRange) with current code). I have also tried to switch between this and Random.new (which gave different numbers but was not random). I have tried to search about this issue online as well and replaced math.randomseed(os.time()) with math.randomseed(time() * tick()) .

Chunk of relevant code:

function spawnPart(randomRange, randomPositionRange, parttospawn)
	for i = 1, Random.new():NextInteger(1, randomRange), 1 do
		math.randomseed(time() * tick())
		wait(math.random(1, 5))
		
		local randomNum1 = math.random(1, randomPositionRange)
		local randomNum2 = math.random(1, randomPositionRange)
		
		print(tostring(randomNum1))
		print(tostring(randomNum2))
		
		local clonepart = storage[parttospawn]:Clone()
		clonepart.Parent = workspace
		clonepart.Position = Vector3.new(randomNum1, 50, randomNum2)
	end
end

Examples of function that use the code:

function startRain()
	spawnPart(30, 100, "Raindrop")
end

function startAcidRain()
	spawnPart(30, 100, "AcidRaindrop")
end

The issue is the tick() in the math.randomseed. I can’t really tell why but I’d suggest you to remove the line completely

By setting a seed you’ll create the same random sequence each time but in this case you’re giving it what should be a some what random seed*, which kinda defeats the purpose of using a seed. Random will seed itself randomly if you don’t specify a seed.

You want to use a seed when you want the “random” values from random to be the same between runs. Most often used for procedural world generation so you can generate the same map each time just by providing a known seed to the generator.

Random seed*, I’m pretty sure time() and tick() are the same thing just different units, so they will both “grow” at the same rate from launch. Each time you run you’re just multiplying the duration of the the run time by itself. That being said it seem unlikely that the script would be executing at exactly the same time each run so the seed should vary some what which should have given at least a few different values to randomNum1 and randomNum2 over the course of at few launches.

That aside, another possible issue is that both randomNum1 and randomNum2 are being set at the same time during the same tick and both get the same answer from random each time. I’m not sure just how random in Roblox deals with such issues. I’ve bumped into this with random in Unity and ended up using multiple C# random instances that each had their own thread, but that’s a different story altogether. From what I can see here Roblox Random, it may just be a case of asking for random.new() for each randomNum. This post explains it a bit.

local randomNum1 = Random.new(1, randomPositionRange)
local randomNum2 = Random.new(1, randomPositionRange)

Or you need to call on

Random.NextNumber(1, randomPositionRange)

In looking at it further by doing

math.random(1, randomPositionRange)

I think you may just be setting the seed to 1 each time with randomPositionRange is just being ignored.

Try

math.random.NextInterger(1, randomPositionRange) 

EDIT: changed NextNumber to NextInterger above

If the values still don’t change you could try setting at least one of the randomNums before calling the function then pass them in as an argument and see what happens.

spawnPart(randomRange, randomPositionRange, parttospawn, randNum1, randNum2)

As @yoshicoolTV suggests drop the seed first and see what happens.

You’re setting the seed each loop. Seeds are best set outside any intense loop using random or they tend to repeat … This is an easy fix and a good random loop set up. You were 99% spot on.

local function spawnPart(randomRange, randomPositionRange, partToSpawn)
    math.randomseed(tick())
    
    for i = 1, Random.new():NextInteger(1, randomRange) do
        wait(math.random(1, 5))
        
        local randomNum1 = math.random(1, randomPositionRange)
        local randomNum2 = math.random(1, randomPositionRange)
        
        print(randomNum1)
        print(randomNum2)
        
        local clonepart = storage[partToSpawn]:Clone()
        clonepart.Parent = workspace
        clonepart.Position = Vector3.new(randomNum1, 50, randomNum2)
    end
end

local function startRain()
    spawnPart(30, 100, "Raindrop")
end

Edit: to be clear. “Seeds are best set outside any intense loop”, is not really just “best”. This IS how you use a seed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.