#I get the "stack overflow" error

1 messages · Page 1 of 1 (latest)

wanton rune
#

I want to make this function that chooses 1 random number from each number range and then adds those numbers up and prints the results, but i get the "stack overflow" error and nothing is printed.

hearty maple
#

use math.random\

wanton rune
#

Where am I supposed to put "math.random" ?

hearty maple
#

num1 = math.random(2,4)

wanton rune
#

Ok, ill try that

#

I did math.random but I am still getting the "stack overflow" error

paper rock
wanton rune
#

Ok, but how am I supossed to fix it?

paper rock
wanton rune
#

Well, I am new to scripting so this is how I tought it should be done

paper rock
# wanton rune Well, I am new to scripting so this is how I tought it should be done

Well this is just quite incorrect on a few levels:

  1. Immediately overwriting the given parameters
  2. for loop is just wrong (num1 & num2 are going to be used as variables, not their values)
  3. pairs wouldn't work on the number you're returning from the function

So lets do this way differently.

If num1 is one range, and num2 is another range, you can just use math.random(<min>, <max>) to get a random number within that range, so you'd just print the sum of two randoms. No loop needed.

wanton rune
#

Thanks because it works, but I was also planning on making it so every possible combination of numbers within those ranges are going to be calculated and the results are going to be printed out

opal oarBOT
#

studio** You are now Level 1! **studio

tough escarp
#

Do a for loop that starts at min and end at max, and nest another one inside there and do the calcs in that loop

wanton rune
#

I am not getting any errors, but it's printing just a number in the output

wanton rune
#

I reviewed the document you sent here but in the output the numbers 3 and 8 are being printed out 5 times

paper rock
# wanton rune I reviewed the document you sent here but in the output the numbers 3 and 8 are ...

Okay, so here you're calculating num1 & num2 in the beginning (once again overwriting the parameters of the function, why are you doing that?) I'm not sure why you're doing this.

The first for goes from 2 - 6

The second only does 2 & 6, as that's what's in the table you give pairs.

The second for loop is still wrong, as you're not going to get what you think you are.

Tables are a set of key, value pairs. In an array-like table, the key is index, the value is the value at that index.

So your second for loop, num1 (which is overwriting the variable) is going to be the key of that list, or index. num2 (also overwriting the original variable) will be 2 & 6.

So it'd be (1, 2) & (2, 6), nothing else.

wanton rune
#

Where should I put num1 and num2?

paper rock
wanton rune
#

well, they are the variables that tell the script what is the first and the second number

paper rock
wanton rune
#

I did this and its finally working, thanks for the help!