Seed from String

   3056   5   0
User Avatar
Member
79 posts
Joined: Feb. 2008
Offline
I'm wondering if there's an easy way to generate a float or int value from a string to use as a seed in a random function?

I usually use like @ptnum or @primnum as seeds in the rand() function to generate random numbers per point or packed primitive or whatever, but now we have a situation where we have an asset that have a unique string path attribute per mesh and this asset will continuously be updated by adding new meshes or removing some. That means certain meshes will have shifting @primnum values as new meshes are inserted or removed and that will change the random value that comes out from the function if I use @primnum as a seed. The path string attributes though will stay consistent. So it would make more sense to use that path attribute to drive the random number generation, but the rand() function only takes in float values. So I'm looking for a way to generate a unique float (or int) value from a string so I can use that value as the seed per mesh and assure consistent random values as our assets get updated.

Does anybody have any suggestion on how I could manage that?
User Avatar
Member
8539 posts
Joined: July 2007
Online
you can try this function
https://www.sidefx.com/docs/houdini/vex/functions/random_shash.html [www.sidefx.com]
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
32 posts
Joined: Aug. 2011
Offline
You can use:

random_shash("string")

https://www.sidefx.com/docs/houdini/vex/functions/random_shash.html [www.sidefx.com]
User Avatar
Member
2038 posts
Joined: Sept. 2015
Offline
With random_shash you have to do a bit of work if the strings you are using as reference are long.
Here's something that can limit the incoming string to specific lengths so it is easier to get consistent results with more variety of string imput.

Dive in geo node and switch wrangle to detail view and play with settings to see results in geometry spreadsheet.
Edited by BabaJ - Feb. 17, 2021 12:23:18

Attachments:
Random_Float_From_String.hiplc (85.6 KB)

User Avatar
Member
79 posts
Joined: Feb. 2008
Offline
Great! That works perfectly.

Thanks guys.

BabaJ, I'm not sure what you consider long strings, but my path strings are between 100 and 200 characters and using this piece of code works perfectly for my needs:
int seed = random_shash(s@path);
@rnd = rand(seed);

But thanks for the example. I will do it your way if ever it becomes an issue.
Edited by MathieuLeclaire - Feb. 17, 2021 14:48:36
User Avatar
Member
2038 posts
Joined: Sept. 2015
Offline
MathieuLeclaire
BabaJ, I'm not sure what you consider long strings, but my path strings are between 100 and 200 characters and using this piece of code works perfectly for my needs:

Yeah, never used random_shash before so I tried it. Yet the docs say for rand the argument is a float not an int. Yet as you say, works fine with large integer variations(which is what random_shash does).

So what I thougth was necessary is to convert that to a fractional float value. I could be remembering wrong but I thought I once used a function in which unless I converted the integer to a fractional value I would get no variation in the result no matter what variation in whole integer values.
  • Quick Links