GDL
About building parametric objects with GDL.
SOLVED!

random 1 or -1 ?

gaba
Enthusiast

some simple randomization algorithm in GDL to get the value 1 or -1 ? 🙂

"When you seek knowledge, you will eventually find it. Then you will become its source - until such time as you must seek it again."
AC 7 - 27 (28), Twinmotion | Windows 10 Pro for Workstations
Dell 7740 Xeon | 64GB | RTX 3000 6GB + LG 2560x1080
2 ACCEPTED SOLUTIONS

Accepted Solutions
Solution
Barry Kelly
Moderator

There is a random function in GDL (RND), but it returns a positive number.

You would have to do something like this I would think...

 

if RND(1) <= 0.5 then

my_value = -1

else

my_value = 1

endif

 

Barry.

 

 

One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11

View solution in original post

Solution

thanks Barry, this is a very old post but the idea is right,

https://community.graphisoft.com/t5/Libraries-objects/Random-numbers-in-2d-script/m-p/22771/highligh...

except that GLOB_INTID is now probably GLOB_INTGUID.
And GLOB_INTGUID is a string and I think you should randomly choose a number (or letter) from it. And I used this fact (letter or number) as a trick for the -/+1 situation (i.e. the direction of decreasing or increasing the constant size of the base object).
Finally, something like this came out:

 

 

 

... myscript

q=strsub(GLOB_INTGUID,int(RND(10)),1) !!! STR random number [0-9] or letter from object GLOB_INTGUID

id2int=0 : n = SPLIT (q, "%n", id2int) !!! convert STR 2 INT

xdir= ( (id2int<EPS)*(-1) + (id2int>EPS)*(1) ) !!! if letter= -1, if number= +1

	!!! default size
	aa=A
	bb=B
	hh=ZZYZX

if rnd_size then	!!! if random object sizes ON

	if rnd_a>EPS then \	!!! if random object size A
			aa = A + RND ((rnd_a - id2int) *0.01 ) * xdir !!! if random A, rnd_a=myrange [0,....]

	if rnd_b>EPS then \ !!! if random object size B
			bb = B + RND ((rnd_b - id2int) *0.01 ) * xdir !!! if random B, rnd_b=myrange [0,....]

	if rnd_h>EPS then \ !!! if random object size ZZYZX
			hh = ZZYZX + RND ((rnd_h - id2int) *0.01 ) * xdir !!! if random ZZYZX, rnd_h=myrange [0,....]
		
endif

...model hedge aa x bb x hh

 

 

 

Control of the range of increasing/decreasing dimensions AxBxZZYZX is done with separate parameters rnd_a, rnd_b, rnd_h. Setting e.g. rnd_a=0 means using the base dimension A.

And generally speaking, in my specific application it is not a "bug" that random numbers are "constant", because I do not want objects to randomly change their dimensions with each window regeneration. It would be bad if a given hedge in one camera shot had for example a different height, and in another camera shot (or section/elevation...) a different one. That would be ridiculous! The random dimensions will actually change only after changing the base AxBxZZYZX (OR AFTER CLONING the object!).

 

rnd_size2.png

 

"When you seek knowledge, you will eventually find it. Then you will become its source - until such time as you must seek it again."
AC 7 - 27 (28), Twinmotion | Windows 10 Pro for Workstations
Dell 7740 Xeon | 64GB | RTX 3000 6GB + LG 2560x1080

View solution in original post

9 REPLIES 9
Solution
Barry Kelly
Moderator

There is a random function in GDL (RND), but it returns a positive number.

You would have to do something like this I would think...

 

if RND(1) <= 0.5 then

my_value = -1

else

my_value = 1

endif

 

Barry.

 

 

One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11

And from memory it is not truly random.

You will get the same result every time you run it.

 

Barry.

One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11

Barry thanks, I know rnd() but it's a weird function.

I thought to use REQUEST('DateTime'... but it's not possible to read at least milliseconds etc.

"When you seek knowledge, you will eventually find it. Then you will become its source - until such time as you must seek it again."
AC 7 - 27 (28), Twinmotion | Windows 10 Pro for Workstations
Dell 7740 Xeon | 64GB | RTX 3000 6GB + LG 2560x1080
Lingwisyer
Guru

I have used the RND function in conjunction with a position variable of the relevant entity before. Use the % operand to "limit" the output to your desired range.

 

 

mat_array[int(rnd(abs(mat_seed*iDis)) % mat_count) + 1]

 

If I want a different generation, I can just change the seed if I do not want to change the position.

 

ps. What are you trying to do that needs to be randomised every milisecond? There's not Unix time stamp in AC...

 

AC22-28 AUS 3110Help Those Help You - Add a Signature
Self-taught, bend it till it breaksCreating a Thread
Win11 | i9 10850K | 64GB | RX6600 Win11 | R5 2600 | 16GB | GTX1660

Thank you @Lingwisyer
1. I have several hedges with the SAME dimensions A,B,ZZYZX e.g. 100x60x150
2. In gdl (without changing A,B,ZZYZX) I would like each hedge to add to A,B,ZZYZX (or subtract) a random value from the range e.g. 0-10cm.
3. This way each hedge will look slightly different (a little longer or shorter, a little wider or narrower, a little higher or lower etc.).

aa=A
bb=B
hh=ZZYZX
if rnd_size then
  if rnd_a>EPS then aa=aa+rnd(10)*rnd_a*0.01 !!! if random A, rnd_a=<0,1,2...>
  if rnd_b>EPS then bb=bb+rnd(10)*rnd_b*0.01 !!! if random B, rnd_b=<0,1,2...>
  if rnd_h>EPS then hh=hh+rnd(10)*rnd_h*0.01 !!! if random ZZYZX, rnd_h=<0,1,2...>
endif
...model hedge aa x bb x hh

For now I can randomly increase A,B,ZZYZX, that's why I asked about random +/-1

rnd_size.png

 

"When you seek knowledge, you will eventually find it. Then you will become its source - until such time as you must seek it again."
AC 7 - 27 (28), Twinmotion | Windows 10 Pro for Workstations
Dell 7740 Xeon | 64GB | RTX 3000 6GB + LG 2560x1080
Yves
Advocate

Another possibility

 

xx = 0.50
my_value = -xx + RND(xx*2)

Yves Houssier
Belgium
Archicad 19 -> 24
iMac - Mac Os 10,13
Lingwisyer
Guru

Need to work on your pruning skills!

 

Are you just wanting the Â±1 to mirror your output values into the negatives? Just double the size of your RND range and offset everything. As Barry has mentioned, every instance of RND in your script will return the same base value, so in your case your range would end up split in half. It is why the code that I showed before uses a position value as it is something that would change enough with each loop to faux the randomisation. The seed value then just makes this faux even more obscure. Without it I would have gotten a very visible set pattern.

 

 

Ling.

AC22-28 AUS 3110Help Those Help You - Add a Signature
Self-taught, bend it till it breaksCreating a Thread
Win11 | i9 10850K | 64GB | RX6600 Win11 | R5 2600 | 16GB | GTX1660
Barry Kelly
Moderator

I found this post that may help to generate non-repeating random numbers.

 

https://community.graphisoft.com/t5/Libraries-objects/Random-numbers-in-2d-script/m-p/22771/highligh...

 

You can read the whole thread but from the post I link to I think is most relevant.

It is all about generating a unique seed.

 

Barry.

 

One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11
Solution

thanks Barry, this is a very old post but the idea is right,

https://community.graphisoft.com/t5/Libraries-objects/Random-numbers-in-2d-script/m-p/22771/highligh...

except that GLOB_INTID is now probably GLOB_INTGUID.
And GLOB_INTGUID is a string and I think you should randomly choose a number (or letter) from it. And I used this fact (letter or number) as a trick for the -/+1 situation (i.e. the direction of decreasing or increasing the constant size of the base object).
Finally, something like this came out:

 

 

 

... myscript

q=strsub(GLOB_INTGUID,int(RND(10)),1) !!! STR random number [0-9] or letter from object GLOB_INTGUID

id2int=0 : n = SPLIT (q, "%n", id2int) !!! convert STR 2 INT

xdir= ( (id2int<EPS)*(-1) + (id2int>EPS)*(1) ) !!! if letter= -1, if number= +1

	!!! default size
	aa=A
	bb=B
	hh=ZZYZX

if rnd_size then	!!! if random object sizes ON

	if rnd_a>EPS then \	!!! if random object size A
			aa = A + RND ((rnd_a - id2int) *0.01 ) * xdir !!! if random A, rnd_a=myrange [0,....]

	if rnd_b>EPS then \ !!! if random object size B
			bb = B + RND ((rnd_b - id2int) *0.01 ) * xdir !!! if random B, rnd_b=myrange [0,....]

	if rnd_h>EPS then \ !!! if random object size ZZYZX
			hh = ZZYZX + RND ((rnd_h - id2int) *0.01 ) * xdir !!! if random ZZYZX, rnd_h=myrange [0,....]
		
endif

...model hedge aa x bb x hh

 

 

 

Control of the range of increasing/decreasing dimensions AxBxZZYZX is done with separate parameters rnd_a, rnd_b, rnd_h. Setting e.g. rnd_a=0 means using the base dimension A.

And generally speaking, in my specific application it is not a "bug" that random numbers are "constant", because I do not want objects to randomly change their dimensions with each window regeneration. It would be bad if a given hedge in one camera shot had for example a different height, and in another camera shot (or section/elevation...) a different one. That would be ridiculous! The random dimensions will actually change only after changing the base AxBxZZYZX (OR AFTER CLONING the object!).

 

rnd_size2.png

 

"When you seek knowledge, you will eventually find it. Then you will become its source - until such time as you must seek it again."
AC 7 - 27 (28), Twinmotion | Windows 10 Pro for Workstations
Dell 7740 Xeon | 64GB | RTX 3000 6GB + LG 2560x1080

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!