How would i make stuff spawn inside a limited range without using math.random or random.new

Recently, i started working on a RNG game where you get cubes (based off another one because they challenged me to make it while using less code) and i want to the cubes to spawn in the baseplate no matter the size

  1. What do you want to achieve? Keep it simple and clear!
    I already made a system for the spawning, but i dont want it to use math.random or random.new, i want it to always spawn inside the baseplate even if i resize it
  2. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Sadly, i cant find any solutions, i tried to math.random using the size but it didnt seem to work

How would i be able to make it?

Fun fact: apparently they used 600 lines for the RNG… i made a really simple system under 50… actually way less

Why exactly you don’t want to use math.random? Cause that seems like the most obv variant for me where you pick 1 number for x and one for z where max and min are based on sizes of baseplate

I did try that, it would spawn outside the box anyways (i also said i tried that in the post)

local b=workspace.Baseplate s=b.Size p=b.Position d=tick()%1
function r()d=(d*9301+49297)%233280 return d/233280 end
c=Instance.new('Part',workspace) c.Size=Vector3.one*2 c.Anchored=true
c.Position=Vector3.new(p.X-s.X/2+s.X*r(),p.Y+s.Y/2+1,p.Z-s.Z/2+s.Z*r())
1 Like

Well, I think I did it

local basePart = workspace.BasePart      -- Platform
local templatePart = workspace.Cube -- The part I used

local function spawn()
	-- Size
	local baseSize = basePart.Size
	local spawnSize = templatePart.Size

	-- Spawn range
	local xRange = (baseSize.X - spawnSize.X) / 2
	local zRange = (baseSize.Z - spawnSize.Z) / 2

	-- Randomise
	local offsetX = math.random() * 2 * xRange - xRange
	local offsetZ = math.random() * 2 * zRange - zRange

	-- New CFrame
	local baseCFrame = basePart.CFrame
	local spawnHeight = (baseSize.Y + spawnSize.Y) / 2

	local newCFrame = baseCFrame * CFrame.new(offsetX, spawnHeight, offsetZ)

	-- Clones the part
	local newPart = templatePart:Clone()
	newPart.CFrame = newCFrame
	newPart.Parent = workspace
end

wait(5)
while true do
	wait(1)
	spawn()
end
1 Like

Just so you guys know, the parts are stored in the script to spawn them, i might use math.random though… as i dont think theres another way (Thanks though, ill try out the code, with edits… of course)

Turns out i just had to /2 the size… (actually a little bit more than that so that it doesnt go on the barrier)

All you said was you can’t use math.random or random.new… You didn’t say I couldn’t make up my own random… That’s 4 lines and enough mathmagic to keep anyone guessing for a bit…

1 Like

Yea i didnt say that, you can still use your own random!

1 Like

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