Math.random aint working

So i got this problem.

math.random is returning the same value in a loop every time the loop is called.

Video of the problem:

you see how the bars are all moving with each other? I want that to be more random. Here is my script:

while true do
	local randomSong = songs[math.random(1,#songs)] --disregard this; for my custom song choosing system
	randomSong:Play()
	randomSong.Playing = true
	titleLabel.Text = randomSong.Title.Value
	artistLabel.Text = randomSong.Artist.Value
	wait(.1)
	while randomSong.Playing == true do --I use "while do" here
		local cam = workspace.CurrentCamera
		cam.FieldOfView = randomSong.PlaybackLoudness/160+70 --Pulsing the camera. Works perfectly.
		
		while wait() do --Each label's size is being changed... by the same value?				here is where I think it is messing up  \/
			script.Parent.Parent.EQ.neg35.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/190*math.random(1.02,1.5) + math.random(.039,.845),0)
			script.Parent.Parent.EQ.neg25.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/195*math.random(1.02,1.5) + math.random(.039,.845),0)
			script.Parent.Parent.EQ.neg5.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/200*math.random(1.02,1.5) + math.random(.039,.845),0)
			script.Parent.Parent.EQ.pos5.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/210*math.random(1.02,1.5) + math.random(.039,.845),0)
			script.Parent.Parent.EQ.pos25.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/230*math.random(1.02,1.5) + math.random(.039,.845),0)
			script.Parent.Parent.EQ.pos35.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/220*math.random(1.02,1.5) + math.random(.039,.845),0)
		end --Dont think I actually need the "while wait() do"

		if randomSong.TimePosition == 0 then --disregard
			randomSong.Playing = false
		end
		wait()
	end
	wait(3)
end

the non-included variables do not matter right now.

You’re setting a variable to math.random. You have to call it every time to get a different result.

math.random cannot return decimals. Try setting math.random(a, b)/10e(x), where x is the amount of decimals. On a second note, (x) should not include parenthesis.

1 Like

where? I don’t think I am. I know not to set math.random to variables.

okay. lemme try this real quick

It should work when you are not including the parenthesis. Alternatively, replace 10e(x) with math.pow(10, x)

problem is, I don’t think that is changing the result of the math.random.
image

I do understand what you are saying though:
image
thats what I would like, but as you said, math.random does not include decimals. I think it is automatically choosing 0 and dividing that by 10e3, which equals 0. The bars moving is the PlaybackLoudness, i presume.

You’re missing the point of the so-called scientific numbers. The 10e3 in scientific notation is equivalent to 1000, but I think the confusion lies in the e, which is supposedly to be E.

while wait() do --Each label's size is being changed... by the same value?				here is where I think it is messing up  \/
	script.Parent.Parent.EQ.neg35.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/190*math.random(1.02,1.5) + math.random(.039,.845),0)
	script.Parent.Parent.EQ.neg25.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/195*math.random(1.02,1.5) + math.random(.039,.845),0)
	script.Parent.Parent.EQ.neg5.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/200*math.random(1.02,1.5) + math.random(.039,.845),0)
	script.Parent.Parent.EQ.pos5.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/210*math.random(1.02,1.5) + math.random(.039,.845),0)
	script.Parent.Parent.EQ.pos25.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/230*math.random(1.02,1.5) + math.random(.039,.845),0)
	script.Parent.Parent.EQ.pos35.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/220*math.random(1.02,1.5) + math.random(.039,.845),0)
end

This part of the script just loops forever because the condition of the while loop is never false.

thank you very much for helping me out. I couldn’t have done it without you :slight_smile:

Hence why I said in the comment that you removed that I didn’t actually need it?

It was mainly for testing purposes:

You might as well use math.pow, as it seems easier to manage, but that’s up to you. I just cleared the readability trouble by setting this variable.

local EQ = script.Parent.Parent.EQ

while wait() do -
	EQ.neg35.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/190 * (math.random(102, 150) / math.pow(10, 2)) + (math.random(39, 845) / math.pow(10, 3)), 0)
	EQ.neg25.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/195 * (math.random(102, 150) / math.pow(10, 2)) + (math.random(39, 845) / math.pow(10, 3)),0)
	EQ.neg5.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/200 * (math.random(102, 150) / math.pow(10, 2)) + (math.random(39, 845) / math.pow(10, 3))),0)
	EQ.pos5.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/210 * (math.random(102, 150) / math.pow(10, 2)) + (math.random(39, 845) / math.pow(10, 3)),0)
	EQ.pos25.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/230 * (math.random(102, 150) / math.pow(10, 2)) + (math.random(39, 845) / math.pow(10, 3)),0)
	EQ.pos35.Size = UDim2.new(.166,0, randomSong.PlaybackLoudness/220 * (math.random(102, 150) / math.pow(10, 2)) + (math.random(39, 845) / math.pow(10, 3)),0)
end
1 Like