# 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 = {
 = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
 = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
 = {-2067.029296875, 2035.0201416016, 9.8203125,90},
 = {2067.0295410156, 1032.1505126953, 10.8203125,90},
 = {1067.0295410156, 2027.8729248047, 10.8203125,90},
}

function getRandomizePos()
local i = math.random(#peds)
local x,y,z,r = peds [i],peds [i],peds [i],depeds lerPeds[i]
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 = {
 = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},
 = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},
 = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},
 = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},
 = {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

Always one index of these indexs got repeated for two times

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 = {
 = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},
 = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},
 = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},
 = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},
 = {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 = {
 = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
 = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
 = {-2067.029296875, 2035.0201416016, 9.8203125,90},
 = {2067.0295410156, 1032.1505126953, 10.8203125,90},
 = {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], peds[rnd], peds[rnd], peds[rnd] )
end
end)
```

i make some changes now work:

```local repeteadIndex = {}

local peds = {
 = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},
 = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},
 = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},
 = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},
 = {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 = {
 = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
 = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
 = {-2067.029296875, 2035.0201416016, 9.8203125,90},
 = {2067.0295410156, 1032.1505126953, 10.8203125,90},
 = {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],peds[rnd],peds[rnd],peds[rnd]
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 = {
 = {1067.0164794922, 2035.6273193359, 10.8203125, 90},
 = {1067.0299072266, 2041.1890869141, 10.8203125, 90},
 = {-2067.029296875, 2035.0201416016, 9.8203125,90},
 = {2067.0295410156, 1032.1505126953, 10.8203125,90},
 = {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],createPeds[rnd],createPeds[rnd],createPeds[rnd]
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