# Unique Random

I want to make a function that generates a random number while excluding a given number.

Example:

generate a random number between 0 - 2 until it is not equal to 1
then return that value

not exactly working....

fn generateRandomNumber = (
rNum = random 0 2
if rNum == 1 do (
generateRandomNumber()
)
return rNum
)

generateRandomNumber()

## Comment viewing options

### Does this seem right?

I'll have to put a check in here incase the min and max values are the same. otherwise it crashes.

arr = #([0,0,0],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5])
disMin = 1
disMax = 4

fn fnRandomLimited arr usedIdx =
(
numItms = arr.count
usedItm = arr[usedIdx]
num = arr[usedIdx]

while num == arr[usedIdx] AND distance num usedItm <= disMin OR distance num usedItm >= disMax do
(
num = arr[random 1 numItms]
)
print (distance num usedItm)
return num
)
clearlistener()

fnRandomLimited arr 4
--find a point three value in the given array which doesn't match the "currently used item in the same array"

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

### Find unused p3 value in array

I don't know, maybe this or i'm wrong.

arr = #([0,0,0],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5])
fn fnRandomLimited arr usedIdx =
(
local p3 = [0,0,0]
while finditem arr arr[random 1 arr.count] != usedIdx do
(
p3 = arr[random 1 arr.count]
format "p3=%\n" p3
)
)
fnRandomLimited arr 4

barigazy

### Check this out. It's what I

Check this out.
It's what I needed it for.
http://jokermartini.com/2012/05/09/cobwebs/

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

### Thanks for you help. I really

Thanks for you help.
I really appreciate it.

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

### Can I ask you what do you

Can I ask you what do you need this stopRandom FN?
Are you still have a problem with the "intersection in time"?

barigazy

### I just saw. For the Cobwebs

I just saw. For the Cobwebs script. Fantastic idea :)

barigazy

### StopRandom FN

Hi John,
If I understood you well, this is what you looking for

fn stopRandom n:3 r1:0 r2:10 =
(
num = 0
x = 3
while num != n do
(
num = random r1 r2
format "num=%\n" num
)
)

barigazy

### Why the x = 0?Here is a

Why the x = 0?

Here is a revision to it.
Check it out.

fn fnUniqueRandom range:[0,3,2] =
(
num = range[3]

while num == range[3] do
(
num = random (range[1] as integer) (range[2] as integer)
format "num=%\n" num
)
return num
)
clearlistener()
fnUniqueRandom range:[0,2,1]

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

### Sorry i forgot to remove

Sorry i forgot to remove it. :)
If you need round float use

num = ceil (random (range.x) (range.y))
--or
num = floor(random (range.x) (range.y))

But you probably know this already:)

barigazy