Random number every run

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.

Regards, Mateo

Hi Mateo,

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

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

Assign your output to the OUT variable.

OUT = r

Should this work?

You need to trigger a force revaluation of the python script :slight_smile: 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!

Hi Mateo,

Not quite :slight_smile:
I believe you still have to evaluate random.random afterward.
Try this:

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

Hi,

I tried as you suggested but it looks like its not working.

Not sure if that is what you meant, I’ll try Jonathan’s suggestion and see if that works.

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…
image

Hi Jonathan,

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.

Thanks for your continued support.

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.

Or use Refinery to do the study for you. :slight_smile:

2 Likes

exactly…

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?

Same as above.

Toggle, player, or Refinery.

1 Like

This seems like oversimplified answer, but have you considered using one of the time nodes to help generate a random number?