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

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
JokerMartini's picture

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)

barigazy's picture

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

bga

JokerMartini's picture

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)

JokerMartini's picture

Thanks for you help. I really

Thanks for you help.
I really appreciate it.

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

barigazy's picture

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"?

bga

barigazy's picture

I just saw. For the Cobwebs

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

bga

barigazy's picture

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

bga

JokerMartini's picture

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)

barigazy's picture

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

bga

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.