Create random numbers in python

Hi everyone!

I’m quite new to python and especially python for dyanmo. What I’m doing requires me to create random numbers inside a python code block but I can’t get it to work. I understand that I probably need to import a library to do this, but I don’t really understand how it works or how I should do it. What I’ve tried so far is to import the .NET reference, but I would like to reference a python library that does this. Any suggestions on how this can be done?

Thanks!

I think this post would guide you to random numbers.

2 Likes

Hi @eriikforsberg,

You have a native Random module in Ironpython :

You need to add the sys.path to import the module.
For example :

import sys
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")
import random
OUT = random.random()

Random%20python

You also have a System Random in .Net:
For example :

import clr
import System.Random
OUT = System.Random(2).Next(1,10)

Net

3 Likes

Thank you so much, works like a charm!

Thank you @SeanP! I’ve looked at this and this code is super useful! I actually even created a component from it so I have it saved!