Ok, so this all started the other evening when my wife was trying to create random teams for an event at work. She needed to select one person from one list to form the head of five other lists, then add to these lists names from another list. Then everyone else needed to be added at random to these lists to make the teams.
My wife being an accountant jumped straight for her tool of choice Excel. I decided I could do the same thing in Dynamo. It was time to get random, (or so I thought).
Creating Random lists and extracting the position of that list in Dynamo is quite easy.
Although this script produces the same random sequence each subsequent time you run it, this allows for consistency if for instance you were using it to randomise coloured panels on a curtain wall.
We could also use the following script below:
This uses a “LunchBox” node (See my blog dynamo packages). This allows us to set a random seed, although as before unless you change the seed it will again give us consistent results.
So, by doing the above to each of my wife’s lists then splitting them into five separate lists I was able to produce her teams.
OK so now you are all asking how I plan to try and make millions out of this? Well let’s take this a little further. If I can produce a random list of names, then maybe I could create a random list of 6 numbers from a list of 59. How about a Lottery number generator in dynamo?
First thing I did was to create a simple family with some model text, and created shared parameters to control them.
No for the Dynamo script, the first problem I now have I do not want consistency in the way the code works. Each time I run it I want a different set of 6 numbers. Dynamo as standard will not do this so I needed to use my own Python code.
The script is quite straight forward, but does require you to import a module (random) from the python library’s. Hence the sys.path.append line to tell python where to find all the librarys and dll’s. The ones in the code are the default install path. Then I used my code node to create a list of the 59 required number 1..59 as an imput and the output which I named Result uses the random.sample to pick 6 numbers at random from the list.
I then used a sort node to reorder my number into ascending order.
I then needed to convert the value from a number to a string as this is what Revit needs to change the value of model text.
The above nodes read the value at position 0 then converts it to a string, I repeated the nodes for position 1,2,3,4 and 5.
Next to change the values of the model text in my family. First thing to do is select the family from the project using Select Model Element node, I can then use the Element.SetParameterByName node to assign the values for the each of the numbers in my list.
The number of items in the list and the number of random number can be changed to fit the lottery requirements of other countries.
If you would like to have a play you can download the family and the Dynamo script here.
If you are interested in taking a training course on either Autodesk Revit or Dynamo or want to find out about our consultancy services then contact Graitec today by clicking the button below.