We can also compute the attribute values dynamically, using functions rather than constants. For example, maybe we want to set the x-coordinate to a random value:

circle.attr("cx", function() {
    return Math.random() * w;
  });
  

If you run this example multiple times, you’ll see that the attribute is recomputed as a number random number each time. Unlike Protovis, D3 doesn’t stash these functions internally; they are run once, immediately, and then your code continues. So you can run them again or redefine them however you like.