# Help me with the Random function

## Recommended Posts

Hello guys,

I want to make a random ped creation but, when I use what I had done with in below, each time I restart the resource for the random ped creation one ped of the other peds get combined with the other peds.

Here's what I had done with

```local peds = {
[1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
[2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
[3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},
[4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},
[5] = {1067.0295410156, 2027.8729248047, 10.8203125,90},
}

function getRandomizePos()
local i = math.random(#peds)
local x,y,z,r = peds [i][1],peds [i][2],peds [i][3],depeds lerPeds[i][4]
return x,y,z,r
end

function ()
local x,y,z,rot = getRandomizePos()
peds =  createPed(0,x,y,z,rot)
end)
```

Special thanks to who gonna help

Anyone can give me a hand?

try this:

```local peds = {
[1] = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},
[2] = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},
[3] = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},
[4] = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},
[5] = {x=1067.0295410156,y=2027.8729248047, z=10.8203125,rot=90} -- the last dont have ,
}

function getRandomizePos()
local i = math.random(#peds)
local x,y,z,r = peds[i]["x"],peds[i]["y"],peds[i]["z"],peds[i]["rot"]
outputChatBox("index: "..i.." value x: "..x.." y: "..y.." z: "..z.." rot: "..rot)
return x,y,z,r
end

function ()
local x,y,z,rot = getRandomizePos()
peds =  createPed(0,x,y,z,rot)
end)
```

Edited by Guest

Not working, still the same problem

ok try again, code change

Always one index of these indexs got repeated for two times

what you want do? spawn the 5 peds once time?

what you want do? spawn the 5 peds once time?

No, only 3 peds of 5 so the 3 Peds will keep changing the positions each time the resource started

try this no tested:

```local repeteadIndex = {}

local peds = {
[1] = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},
[2] = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},
[3] = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},
[4] = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},
[5] = {x=1067.0295410156,y=2027.8729248047, z=10.8203125,rot=90} -- the last dont have ,
}

function getRandomizePos()
local i
repeat i = math.random(#peds)

local x,y,z,r = peds[i]["x"],peds[i]["y"],peds[i]["z"],peds[i]["rot"]
outputChatBox("index: "..i.." value x: "..x.." y: "..y.." z: "..z.." rot: "..r)
return x,y,z,r
end

function table.contains(t,value)
for k,v in pairs(t) do
if(v==value)then
return true
end
end
return false
end

function ()
for i=1,3 do
local x,y,z,rot = getRandomizePos()
peds =  createPed(0,x,y,z,rot)
end
end)
```

Edited by Guest

Not working for me, can you test it please to make it working?

```local peds = {
[1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
[2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
[3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},
[4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},
[5] = {1067.0295410156, 2027.8729248047, 10.8203125,90}
}
local createdPeds = {}

for i=1, 3 do
local rnd = math.random ( #peds )
while createdPeds[rnd] do
rnd = math.random ( #peds )
end
createdPeds[rnd] = createPed ( 0, peds[rnd][1], peds[rnd][2], peds[rnd][3], peds[rnd][4] )
end
end)
```

i make some changes now work:

```local repeteadIndex = {}

local peds = {
[1] = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},
[2] = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},
[3] = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},
[4] = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},
[5] = {x=1067.0295410156,y=2027.8729248047, z=10.8203125,rot=90} -- the last dont have ,
}

function getRandomizePos()
local i
repeat i = math.random(#peds)

local x,y,z,r = peds[i]["x"],peds[i]["y"],peds[i]["z"],peds[i]["rot"]
outputChatBox("index: "..i.." value x: "..x.." y: "..y.." z: "..z.." rot: "..r)
return x,y,z,r
end

function table.contains(t,value)
for k,v in pairs(t) do
if(v==value)then
return true
end
end
return false
end

function ()
for i=1,3 do
local x,y,z,rot = getRandomizePos()
local ped =  createPed(0,x,y,z,rot)
end
end)
```

but Bonus code is better

But I need to return the cordinations with a function because I will use it to set blips..etc

@bonus, can you help me please

Anyone?

you can use my code or edit Bonus code like this:

```local peds = {
[1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
[2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
[3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},
[4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},
[5] = {1067.0295410156, 2027.8729248047, 10.8203125,90}
}
local createdPeds = {}

for i=1, 3 do
local x,y,z,rot = getRandomizePos()
-- now you can use x,y,z,rot variables
local ped = createPed (0,x,y,z,rot)
--createBlip(x,y,z)
end
end)

function getRandomizePos()
local rnd = math.random ( #peds )
while createdPeds[rnd] do
rnd = math.random ( #peds )
end
table.insert(createdPeds,rnd)
local x,y,z,rot = peds[rnd][1],peds[rnd][2],peds[rnd][3],peds[rnd][4]
return x,y,z,rot
end
```

Still doesn't work..

Outputing two index of 5 are the same

like: 5,2,2,1,3

My new code:

```local peds = {}
local createdPeds = {}
local createPeds = {
[1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
[2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
[3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},
[4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},
[5] = {1067.0295410156, 2027.8729248047, 10.8203125,90}
}

function getRandomizePos()
local rnd = math.random ( #createPeds )
while createdPeds[rnd] do
rnd = math.random ( #createPeds )
end
table.insert(createdPeds,rnd)
local x,y,z,rot = createPeds[rnd][1],createPeds[rnd][2],createPeds[rnd][3],createPeds[rnd][4]
return x,y,z,rot,rnd
end

function ()
for index, v in pairs(dealerPeds) do
local x,y,z,rot,rnd = getRandomizePos()
local int = rnd
peds[int] = createPed(0,x,y,z)
if peds[int] then
setElementRotation(peds[int],0,0,rot)
end
createBlip(x,y,z,6,2,255,0,0,0,0,600)
end
end
```

## Create an account

Register a new account