Hi, I am trying to make a node that creates a different random number every run. I tried using standard random nodes from dynamo but both the random and random seed nodes will repeat the same result every run, only exception would be to use the random seed and change the seed every run. Anyhow, although changing the seed manually is already a good solution, I would prefer the random number to be generated automatically every time.
Looking in the forums I saw this python code:
Enable Python support and load DesignScript library
import clr
clr.AddReference(âProtoGeometryâ)
from Autodesk.DesignScript.Geometry import *
The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
Place your code below this line
import sys
sys.path.append(râC:\Program Files (x86)\IronPython 2.7\Libâ)
import random
r=random.random()
Assign your output to the OUT variable.
OUT = r
I managed to get the node to work and generate the random number, but as with the dynamo nodes, the number stays the same every run.
Thank you all in advance for the help and I already apologize for the format, itâs my first post in here and didnât know a better way to post the code.
Now assuming that it is not because Dynamo shortcuts (i.e. since youâve performed a run before with no changes to the input and therefore Dynamo doesnât reevaluate the python script) and youâve tried to close the script and rerun it sounds like you have the same seed every time you run your script.
One way to combat this could be to use your current date time as your seed.
Please see the example below. I hope it helps!
import random
from datetime import datetime
random.seed(datetime.now())
Hi Lukas,
thank you very much for the answer. Still I havenât managed to make it work. The problem I think is that I am new to coding and I have no clue what I am doing XD. Mainly I make changes and press run and see what happens.
I tried to implement the code you suggest but I canât make it work. This is what I tried:
import clr
clr.AddReference(âProtoGeometryâ)
from Autodesk.DesignScript.Geometry import *
The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
Place your code below this line
import sys
sys.path.append(râC:\Program Files (x86)\IronPython 2.7\Libâ)
import random
from datetime import datetime
r=random.seed(datetime.now())
You need to trigger a force revaluation of the python script you could input the datetime from datetime node into e.g. IN(1) of the python, you do not need to do anything with it in your script!
Firstly since none of you are posting preformatted text⌠You get an error⌠as â and â are not the same symbolâŚ
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import random
from datetime import datetime
random.seed(datetime.now())
r=random.random()
OUT = r
Here is what I suggested⌠Try it with periodic run enabledâŚ
it works as you said, thank you very much. Following problem I have is that I use the number to create a seed to randomize a whole script. The idea is that I want the script to generate some randomized design solutions. If the design isnât liked by the designer he can run it again to generate a new variant. I am afraid that having the script run periodically wonât work. What I donât get is why the DateTime input wonât update every run⌠If you use it once it will generate the timestamp to the last second. If you run the script several times, the first value will be re-used⌠Kind of strange for me.
Because no change is made, add a bool that can changed and have that fed Into the python instead, the designer then need to press the bool if a new value/design is to be generated
The âwhen triggeredâ value is more likely what you really want. âOh thatâs perfect but can we change that color on the display?â wonât require you stumbling into the right value again this way.
I also recommend cataloging the values used Incase you want them back. Appending to a CSV works for this. Or write to a separate file on each run.
since I am facing a similar problem, is there a known solution for this type of problem (generating a random value of any kind every time you hit ârunâ) by now?