# Tutorials

## Learn new skills with our bite sized lessons

Non-Repeating Randomisation
Learn how to avoid repeating adjacent segments with this handy tip.
Technologies:
RailClone

#### Requirements

RailClone Pro

In this quick-tips tutorial we're going to answer a common question, "how can I randomise items so that adjacent segments and materials IDs are not repeated?". It is the nature of true random generators that they will occasionally output a sequence with repeating values. This can be undesirable when it results in repeating material IDs or geometry. Unfortunately neither RailClone's built in Random node nor its expressions features have an option to output a non-repeating sequence, but there is a nifty cheat we can use to create a similar effect.

To illustrate, we'll create a play mat style that features 10 numbered segments. Each segment uses two material IDs: ID 1 is used for the colour of the tile, and ID 2 is for the number.

If we distribute the segments along a single spline using a L1S Generator via a Randomise Operator you will see that occasionally adjacent segments are the same.

To fix this divide the number of segments in half so (in this example) you have 2 groups of 5 segments each. These two groups are placed alternately to avoid repetition. To do this:

1. Create two Randomise operators.
2. Wire half of the segments to the first Randomise operator.
3. Wire the remaining segments to the second Randomise operator
4. Create a new Sequence operator. We can use this to alternate between the two Randomise operators.
5. Wire the Randomise operators to the Sequence operator's inputs.
6. And finally wire the Sequence operator to the Generator.

You now have no repetition because odd items are randomising between segments 1 - 5 and even items are randomising between segments 6-10. Repetition on adjacent segments is impossible!

This works great for 1D arrays, but what about 2D arrays? If we wire the same setup to an A2S generator you'll see that there is no repetition horizontally along each row, but an awful lot of repetition vertically, in columns.

Fortunately this too is quite easy to fix. Remember odd items use segments 1-5 and even items use segments 6-10. If we switch the items being used in odd and even segments on alternate rows we will create a grid of randomised segments that doesn't repeat horizontally or vertically. The table below makes this clearer.

...1
(odd)

2
(even)

3
(odd)

4
(even)

4
(even)
6 - 101 - 56 - 101 - 5
3
(odd)

1 - 5

6 - 101 - 56 - 10
2
(even)
6 - 101 - 56 - 101 - 5
1
(odd)

1 - 5

6 - 101 - 56 - 10

To create this setup:

1. Duplicate the existing Sequence operator.
2. Switch the order of the inputs in the new Sequence operator by clicking on the small black arrows next to the Input label.

3. Create a third Sequence operator and wire the two existing Sequence operators to its inputs.
4. Finally change the third Sequence operator's Increment At axis to Y.

You will no longer see any repetition between adjacent horizontal and vertical segments!

### Non-repeating material IDs

Exactly the same technique can be used to randomise non-repeating material IDs. To make this a little easier, recent versions of RailClone include a macro to do all the hard work for you. To use the macro:

1. Switch the the Macros Tab and drag a PseudoRandomise IDs 2D macro into the graph.
2. Wire the macro between the final Sequence operator and the Generator.
3. Go to the Properties panel and in the Material ID property, enter the ID you wish to Randomise. In this case we'll leave it at 1 to randomise the colour of the tile.
4. Use the From and To properties to set the range. In this example From 1 To 10.
5. You'll now have non-repeating randomised colours on the tiles.
6. You can do the same thing to randomise the colour of the numbers. Just add another macro but this time use a value of 2 for the Material ID property.

### Using a macro to randomise non-repeating geometry

In the scene file that accompanies this tutorial there's a macro called NonRepeatRandomInt. This will also be included in our next update to the built-in macros. It works using exactly the same principle as the examples shown above, except it outputs non repeating integers based on a segment's position in the 2D  grid. This means you can use it with anything that is controlled using an index. For example, to randomise non-repeating geometry using this macro instead of using random and sequence operators as we saw earlier, we can simply do the following:

1. Create a new Selector operator and wire all of the segments to its inputs.
2. Right-click on the operator and export the Index property.
3. Attach the Macro to the Index input.
4. Leave Start Value at 1 and enter the total number of segments for the End Value.

5. That's it! The macro takes care of everything else for you.

Though this is a bit of a hack, it's quite effective for preventing the repetition of the same geometry, materials and more on adjacent segments. In this tutorial we also explored a couple of recent macros, in future releases we'll look in more depth at several other macros that can be used to speed up and simplify your work, in addition to extending the plugin's core features.