Generating a random value...

   5008   6   2
User Avatar
Member
319 posts
Joined:
Offline
Hi all you Houdini Wizards,

I'm just wondering, is there a function for generating a completely random output? In my expressions I am frequently using the rand() function, but as you guys probably know this is not truly random, as it is dependent on its argument/parameter. Even if you use a changing value such as $T or $F etc. you will get a changing value but the sequence will always be the same…..so is there another function which generates a truly random output? Other languages I have worked with have usually had this function….

I have looked through the help and there seems to be a number of ways of generating true random numbers in VEX but I can't see any expression functions that do the same?

Cheers,

Deano.
User Avatar
Staff
2591 posts
Joined: July 2005
Offline
Dean_19
I'm just wondering, is there a function for generating a completely random output? In my expressions I am frequently using the rand() function, but as you guys probably know this is not truly random, as it is dependent on its argument/parameter.

Typically, you want to be able to reproduce random numbers since you want animation to be reproducable.

However, if you do want a different value every time you call rand(), you just have to make sure that the seed to the rand() function is different each time you call it… The system() function can be used (though it can be a bit expensive). For example, on Linux, you might do something like:
rand(system(“date +%N”))
would provide a different random number for each nano-second in real time… There are only a billion different random numbers which can be returned, but that should be enough.

Oh, and I bet there's something you could do with /dev/random on Linux too – that would really give you random numbers… But how to use that easily escapes me at the moment (without resorting to the HDK that is).
User Avatar
Member
319 posts
Joined:
Offline
Hmm interesting, I will try that, thanks. Just out of interest, if that is a Linux property would that syntax still work with a windows version of Houdini? By expensive do you mean in terms of system resources?

Thanks for your help.

Deano.
User Avatar
Member
4256 posts
Joined: July 2005
Offline
I think this should work… It only works on Linux, (or if you have Cygwin working with Houdini on Windows).

If you really want to fetch stuff from /dev/random


echo `system(“dd if=/dev/random bs=1 count=4 2>/dev/null | od -t u4 | awk ‘NR==1 {print ($2/((2^32)-1))}’”)`

My nerd quota for week has now been reached.
if(coffees<2,round(float),float)
User Avatar
Member
4256 posts
Joined: July 2005
Offline
Dean_19
Hmm interesting, I will try that, thanks. Just out of interest, if that is a Linux property would that syntax still work with a windows version of Houdini? By expensive do you mean in terms of system resources?

Thanks for your help.

Deano.

system() works on Windows or Linux, it just runs an external program. Since the system() function is starting an external program you have the additional overhead of running that command and having it communicate back to Houdini. Using system() in scripts and in some expressions is usually fine. But putting the system() function in an expression on the Point SOP may cause some serious slowdowns on large datasets.
if(coffees<2,round(float),float)
User Avatar
Member
2199 posts
Joined: July 2005
Offline
If you are working in VEX or VOPS you can always use nrandom()
The trick is finding just the right hammer for every screw
User Avatar
Member
319 posts
Joined:
Offline
Thanks for your help guys.

Deano.
  • Quick Links