ScriptSpot

Forum topic · General Scripting

Unique Random

By JokerMartini · 2012-05-09

Description

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()

Comments (9)

Does this seem right?

JokerMartini · 2012-05-09

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"

Find unused p3 value in array

barigazy · 2012-05-10

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

JokerMartini · 2012-05-09

Thanks for you help.
I really appreciate it.

barigazy · 2012-05-09

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

barigazy · 2012-05-09

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

StopRandom FN

barigazy · 2012-05-09

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
)
)

Why the x = 0?Here is a

JokerMartini · 2012-05-09

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]

barigazy · 2012-05-09

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:)