# Shepherding Random Numbers

## Introduction

The following is a tiny guide to shepherding random numbers. Originally I used this as a part of a presentation, but it seemed like it would work well as a little text as well. It does not really deal with statistics or probability, it is simply a collection of a few useful tricks I've learnt for manipulating random numbers. Usually I use the following techniques as a part of a larger work.

This text relies on Javascript animations (Canvas). They should work on most devices, but please let me know if you experience any issues.

You can click or tap all animations to restart them.

## Random Node

We start with something very simple: a single node that moves between random positions.

## Multiple

It is always useful to see what it looks like when we introduce more things. Here are multiple independent nodes that move in the same way as before.

## Memory

The nodes still move at random. However, now they move relative to their previous position, that is: they move slightly up or down from where they are. As opposed to previously when they would move at random to a completely new position. One could say that they have developed a simple kind of memory.

This also makes it look as if all the nodes are very nervous about something.

When we look at this it can sometimes seem as if there is a pattern emerging from the behaviour. However, if you feel like you can see such a pattern it is entirely coincidental, since the nodes are completely unaware of each other.

Whenever a node reaches the edge of the canvas, we stop it's motion. The node will remain at the edge until it should be inclined to start moving in the opposite direction.

## Velocity

We now introduce the idea of velocity. Instead of moving relative to their previous position, nodes will now move according to their current velocity.

To make things a little more interesting we manipulate the velocity in the same way that we did node position in the previous example. That means that the velocities gradually will change. Velocities can be either positive or negative, as you can see indicated by the lines in the animation.

## Sums of Velocities

Until now all the nodes have been completely independent of each other. So here is one possible way to change that. Each node now has two associated velocities. One is the regular velocity. This behaves exactly like in the previous example. The second is the special velocity. The special velocity is re-calculated for each frame, and it is the sum of all the current regular velocities of the nodes on it's left hand side.

If this seems a little arbitrary to you, it is probably because it is. There is no obvious reason to do this, other than the fact that it behaves rather pleasing.

The lines in the animation below indicate the special velocity.

## More Nodes. Again

It is time to introduce even more nodes. Adding more things can help you see thing differently. In this case it makes it a little easier to see the total behaviour of the system of nodes.

## History

For me the goal is frequently to make a static result. That is, something that will make a good image. While these animations are nice and informative, a snapshot of one of them is not really that interesting. A way of getting different results is by drawing behaviour over time. Showing the history of a behaviour like we do below can also be a nice alternative way of understanding what is happening.

## Different Dimensions

So far we have looked at behaviour along a single axis, or dimension. Lets apply the exact same behaviour as before in two dimensions.

## New Beginnings

Things will look quite different if we change the initial distribution of nodes. Below we see what happens if we wrap the nodes along the circumference of a circle.

## More Random

It is always a good idea to experiment with adding more randomness. One example is to distribute the nodes at random along the circumference. The important thing to keep in mind in this case is that since the behaviour is exactly the same as before, there is still the same relation between the nodes' velocities. It is just their initial position that has changed.

You can probably think of a few other things to try randomizing as well.

## Conclusion

These are just a few of the things you can do with random numbers. The next part of this text covers some more techniques: Shepherding Random Grids.

If you made it this far you may also be interested in my essay On Generative Algorithms.