D3: force layout should have option to set random number generator seed

Created on 9 Jan 2013  路  4Comments  路  Source: d3/d3

Because the force layout uses Math.random() to generate random numbers, the layout looks different every time a page is reloaded. It would be useful to add the option to set a random number seed so that there is an option of reproducible layouts.

Most helpful comment

@cmap I know this is old, but thought I'd share something I found. https://github.com/davidbau/seedrandom seems to work pretty well for making the force layout more or less deterministic. Here is a JSFiddle I adapted: http://jsfiddle.net/s8pcecka/2/

Unfortunately, it isn't completely cross-browser. Chrome and Firefox resulted in very similar layouts. Safari was fairly different. Haven't done much IE testing.

All 4 comments

Unfortunately, JavaScript doesn't have a seeded random number generator, and I don't really want to implement one from scratch. Also, due to floating point error and interaction (hover-on-fixed, dragging), even a seeded random number generator would not guarantee deterministic output.

If you want to guarantee a particular layout, I run the force layout locally and then save the _x_ and _y_ coordinates it produces to a file. Then initialize your nodes with these pre-computed coordinates, and the layout will stay in the same position. You can see an example of this technique in the interactive At the National Conventions, the Words They Used.

Could we use http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html instead of Math.random? Unfortunately your workaround doesn't for my particular use case.

@cmap I know this is old, but thought I'd share something I found. https://github.com/davidbau/seedrandom seems to work pretty well for making the force layout more or less deterministic. Here is a JSFiddle I adapted: http://jsfiddle.net/s8pcecka/2/

Unfortunately, it isn't completely cross-browser. Chrome and Firefox resulted in very similar layouts. Safari was fairly different. Haven't done much IE testing.

Wow that is incredible, thanks for sharing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sachi-d picture sachi-d  路  3Comments

Fil picture Fil  路  3Comments

mbostock picture mbostock  路  3Comments

greyman888 picture greyman888  路  5Comments

AndrewRayCode picture AndrewRayCode  路  5Comments