# Randi going out of parameters?

Attention Topic was automatically imported from the old Question2Answer platform.

So basically ive been working on a random terrain generation thats 2d, im not using simplex just yet as im still trying to get a hang of things but basically whenever i try to for loop a randi it gives me out of parameter answers but i think it has something to do with my variables since i do this:

var h = 10
var loop = 5

for x in 5:
var a = h - 1
vat b = h + 1
var random = randi()%b + a

What exactly is happening here and how do i fix it?

I’m not sure what you’re trying to do. What do you mean by out of parameters? Currently the code gives you numbers that are between 9 and 20.

exuin | 2021-04-01 00:06

What is the range expected? This could greatly help in finding the solution.

Bubu | 2021-04-01 07:02

i wanna find a random number that would give me numbers from 9 to 11 while using the
the h as a parameter for a for loop

AwYiss | 2021-04-01 07:16

How come 20? when b should be 11??? also im trying to make a random terrain generation without using simplex but instead using a tile map and for looping.

AwYiss | 2021-04-01 07:19

9 and 20? not 9 and 11? how? also what i mean by out of parameters is it goes from expected maximum which should be 11 and expected minimum which should be 9 no?

AwYiss | 2021-04-01 07:35

To get numbers between 9 and 11 you want `randi() % 3 + 9`. The randi function gives you a random integer and using modulus on it gives you a range of numbers. Then adding to that result will shift the range up.

Ok, i just guess, that you want to get random values from 9 to 11 (10 +/- 1)

``````var a = h - 1
var b = h + 1
var random = randi() % b + a
``````

``````h == 10
a == 9
b == 11
``````

`randi() % 11` gives you values between 0 and 10
then you add 9, so you get something between 9 and 19

So to get from 9 to 11, we need something like 9 + value between 0 and 2

``````var random = a + randi() % 3
``````

to make this usable you should write a function:

``````func generate_terrain_step(height : int, max_diff : int) -> int:
return (height - max_diff) + (randi() % (max_diff * 2 + 1))
``````

then you can use it like this

``````var h : int = 10
var h_diff : int = 1
var loop : int = 5

for x in loop:
var random : int = generate_terrain_step(h, h_diff)
``````

EDIT:

btw, you can also use

``````func generate_terrain_step(height : int, max_diff : int) -> int:
return int(rand_range(height - max_diff, height + max_diff + 1))
``````

but i think the other solution is cleaner

When using `randi()`, make sure that you put `randi() % range + offset`
`var random = randi()%3 + a` should work.