How to generate Random number with a timer (with a specific Seed value)

Hi,

I am trying to make a component in GhPython where I will be using Boolean toggle like a switch to turn it on/off and generate Random Numbers every time it runs. I am also connecting a timer so that I can control the time intervals as well.

  1. Importing Random and has a specific Seed Value
  2. Run the component by a Boolean Toggle
  3. Start with a Random Value (Initial Value)
  4. Increase or Decrease the initial value by adding or subtracting a Random Number in time (Incrementing value). This part will be performed to change the initial value in time.

The issue is - It initiates with a random number which is fine. But in Step-3, I want a different value every time it refreshes/calculates with a timer. Two random values are in play - one is static (initial value) and another one is changing (the incremental one). Per my understanding, it is not happening cause the Seed value is specified, so it is applied on both Step-2 and 3. Actually, I want different Incremental values every time it runs. So, is there a way to tackle this issue or any other way to do it? Would really appreciate that. Thanks!

It won’t give you a different number each time, since you’re using always the same seed (random.seed(1)). You’ll have to pick a different seed each time.

You’ll get a new random number on every call if you don’t set a seed. And you can sum the numbers by setting up a persistent variable (and also script the timer), like so:


200401_RandomNumber_Timer_00.gh (3.7 KB)

Edit: Here’s a version that allows you to set a seed and get a new number at each iteration:


200401_RandomNumber_Timer_01.gh (3.8 KB)

3 Likes

This is indeed a good way to get randomness, and yet have reproducability.

1 Like

Thanks, @AndersDeleuran. Much appreciated the solution.

As I am still learning GHpython some of the moves here are not familiar to me. Just need to the test lines one-by-one for my own understanding. If possible would you be able to point out some of the documentation that I can look into, especially for the “updateComponent” function? Need to understand how those grasshopper libraries were called and how they are structured. Would really appreciate that. Thanks!

1 Like

The updateComponent function is one I wrote a long time ago for developing dynamic models (e.g. when implementing Kangaroo2, developing agent-based systems etc.). I made a few notes with links about this topic the other day over here. Which also covers the second aspect you might find confusing about the script I posted, regarding making persistent variables for e.g. making a counter:

In terms of scripting Grasshopper itself from GHPython, I’m afraid there isn’t much documentation outside of scripts posted on this forum and the old Grasshopper forum. And of course the Grasshopper API/SDK itself (which can be pretty impenetrable to be honest):

https://developer.rhino3d.com/wip/api/grasshopper/html/723c01da-9986-4db2-8f53-6f3a7494df75.htm

Edit: Here’s a great old post by @piac that might help as well:

2 Likes

Thanks, @AndersDeleuran, much appreciated. These are very helpful resources!

1 Like