Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does it give me a 'bad argument #2 to 'random'? - math.random(-(1.5*i), (1.5*i)

Okay, one part of a new game I'm making is a simulated "Stock Market"... The player buys a "stock" at a certain price, and can sell it at either a loss or gain after the script refreshes its value after x amount of time... The amount of times it will loop successfully has varied for me, anywhere from once to 5 times, all of it eventually giving me the same error.

KNG = 250
OCT = 1500
SHL = 2000
SNY = 4350
DEL = 8750
MCR = 25000
a = 0

--Prints new values to console
function updateConsole()
	print("KNG - ", KNG, "OCT - ", OCT, "SHL - ", SHL, "SNY - ", SNY, "DEL - ", DEL, "MCR - ", MCR)
end

--Updates values for each stock
function newValue(stock)
	stock = stock + math.random(-(1.5*stock), (1.5*stock))
	if stock < 0 then
		stock = math.random(-2500, 100) --Keep stock from falling too hard...
	end
	return stock
end

updateConsole() --Prints out initial values (Debug & Pre-GUI)
--Loops new updates 100 times.
while a <= 100 do
	wait(5)
	KNG = newValue(KNG)
	OCT = newValue(OCT)
	SHL = newValue(SHL)
	SNY = newValue(SNY)
	DEL = newValue(DEL)
	MCR = newValue(MCR)
	updateConsole()
	a = a + 1
end

KNG - 250 OCT - 1500 SHL - 2000 SNY - 4350 DEL - 8750 MCR - 25000 KNG - 179 OCT - 508 SHL - 2413 SNY - 6728 DEL - 15466 MCR - 41644 KNG - 430 OCT - 787 SHL - -219 SNY - 13221 DEL - 19273 MCR - 3084 21:40:50.373 - Workspace.Stock Values:16: bad argument #2 to 'random' (interval is empty) 21:40:50.375 - Stack Begin 21:40:50.376 - Script 'Workspace.Stock Values', Line 16 - global newValue 21:40:50.377 - Script 'Workspace.Stock Values', Line 29 21:40:50.378 - Stack End

KNG - 250 OCT - 1500 SHL - 2000 SNY - 4350 DEL - 8750 MCR - 25000 KNG - 134 OCT - 1373 SHL - 1250 SNY - 8878 DEL - 3943 MCR - 21707 KNG - 124 OCT - -1190 SHL - 2120 SNY - 6363 DEL - 1336 MCR - 26184 22:06:51.928 - Workspace.Stock Values:16: bad argument #2 to 'random' (interval is empty) 22:06:51.930 - Stack Begin 22:06:51.932 - Script 'Workspace.Stock Values', Line 16 - global newValue 22:06:51.934 - Script 'Workspace.Stock Values', Line 28 22:06:51.935 - Stack End

I tried running it without lines 17-19 and it did effect the problem at all, other than made it 3 lines shorter for the same error. I also tried changing the stock = stock + math.random(-(1.5*stock), (1.5*stock)) to stock = stock + math.random((-1.5*stock), (1.5*stock)) and got the same result.

Eventually I'm going to make it run as a table, but I'm just getting back into LUA from vb.net and C++, and tables and arrays work differently in LUA, and as I get further in the dev. cycle, this script will definitely be expanded on, this was just a quick little thing I put together trying to see how branching off from this would work. Any help is greatly appreciated!

3 comments

1

Make sure all the math.random() things are arranged in ascending order, on whichever line fires "Interval is empty". They should be formatted as: math.random(lower, higher)

1

It's because math.random takes integers, not floats. I recommend using the math.floor() and math.ceil() functions to round down or up the float values.

0

"interval is empty" means that the first value is greater than the second value. The data type of the numbers does not matter for this error, like SquirrelOnToast said.

2 answers

✓ Accepted answer

I ran your code in a Lua compiler online when I couldn't access ROBLOX, take a look at this: http://i.imgur.com/RV94kjG.png .

The value of KNG, for example, goes from 250 to 260... then to -31. This means that -(1.5*stock) = -(1.5 x -31) is 46.5 and (1.5 x stock) is -46.5, so the interval is empty as 46.5 > -46.5 NOT <. This negativity should be taken into account into the calculation. If you need to convert a negative number into a positive number, or a positive into itself, use the math.abs function.

Hope this has helped. It's not anything to do with the data type of the numbers, e.g. if it's integers or decimals or singles etc.

When you give parameters to math.random, they should be two integers such that the first one is less than the second one.

What you're giving aren't integers (* 1.5) and the first could be bigger than the second (if stock is negative).

You can use math.random(), which produces numbers in the range [0, 1), to get continuous changes:

stock = stock * (-0.5 + 3 * math.random())
-- 1 --> between -0.5 to 2.5
-- 10 --> between -5 and 25

1 comment

0

Hey! Thanks for the answer, this was basically what Climax had put in his answer, but since I can only have one as answered I just did whoever answered first and upvoted you. Thanks for the help!

This discussion is preserved as it was. New answers, comments, and votes are closed.