About probabilistic switch in GH_Python

Here is some code form Processing with IGEO. You can use IRandom.percent() to create a condition which is switched probabilistically by putting percentage in its argument. Can we do it the same in GH_Python?

add_library(‘igeo’)

size( 480, 360, IG.GL )

for i in range(50) :
for j in range(50) :
x = i4-100
y = j
4-100
# 50% probability to be executed
if IRand.pct(50) :
ISurface(x,y,0,x+2,y,0,x+2,y,50).clr(1.,1.,1.)
# 50% of the rest ( = 25%)
elif IRand.pct(50) :
ISurface(x,y,0,x+2,y,0,x+2,y,20).clr(0,1.,1.)
# 50% of the rest of the rest ( = 12.5%)
elif IRand.pct(50) :
ISurface(x,y,0,x+2,y,0,x+2,y,10).clr(1.,0,0)

Hi Daizhuo,

How about the python random library:

import random

for i in range(99):
    if random.randint(0,100) < 50:
        print i

-Willem

Thank you!
So we have to use list to loop for selecting items? Is there a way more simply to do that?

I don’t understand your remark;

What is the difference between your line:

if IRand.pct(50) :

or

 if random.randint(0,100) < 50:

-Willem

in processing, we can just use IRand.pct() to select items for we need, no need to use list.

50% probability to be executed

    if IRand.pct(50) :
        ISurface(x,y,0,x+2,y,0,x+2,y,50).clr(1.,1.,1.)

In this sample, with IRand.pct(), we can select 50% of generated surface! no need to do any other things.

It’s similar to GH_component _Random Reduce.

Do you understand what this code does:

if random.randint(0,100) < 50:

50% of the times it returns True in a random fashion
what other functionality does IRAnd.pct(50) have that I’m missing?

-Willem

Wow! Cool! It works! Sorry for not understanding this method. Thank you very much!

1 Like