<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "PHASE",
  "identifier" : "/documentation/PHASE/PHASERandomNodeDefinition",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "PHASE"
    ],
    "preciseIdentifier" : "c:objc(cs)PHASERandomNodeDefinition"
  },
  "title" : "PHASERandomNodeDefinition"
}
-->

# PHASERandomNodeDefinition

A sound event node that invokes one of its child nodes at random.

```
class PHASERandomNodeDefinition
```

## Overview

When the framework invokes a random node, it passes the invocation on to one of its children at random. The weight you choose for a child node in the [`addSubtree(_:weight:)`](/documentation/PHASE/PHASERandomNodeDefinition/addSubtree(_:weight:)) argument skews the node’s selection chances.

### Choose from Alternate Sounds

This class can model real-world cases where an event varies slightly, such as when footsteps sound slightly different because of the unique ground composition at each step.

![Illustration of a flowchart that represents a sound event node tree. The chart contains three boxes, which represent nodes. At left, a box labeled Random Node extends an arrow, which points to a box in the upper right that’s labeled Sampler Node Footstep Variation One. The box labeled Random Node extends another arrow, which points to a box in the lower right that’s labeled Sampler Node Footstep Variation Two. ](images/com.apple.phase/media-3918860@2x.png)

The following code creates an instance of this class that selects from three different footstep sounds. The weights determine that an uncommon footstep noise plays half as frequently as the common footstep. And a third footstep noise plays 10% of the time.

```objc
// Create several nodes from prior-registered footstep sound assets.
PHASESamplerNodeDefinition* footstep1 = 
    [[PHASESamplerNodeDefinition alloc] 
        initWithSoundAssetUID:@"footstep1" 
        mixerDefinition:mixNode uid:@"footstep1"];
PHASESamplerNodeDefinition* footstep2 = 
    [[PHASESamplerNodeDefinition alloc] 
        initWithSoundAssetUID:@"footstep2" 
        mixerDefinition:mixNode uid:@"footstep2"];
PHASESamplerNodeDefinition* footstep3 = 
    [[PHASESamplerNodeDefinition alloc] 
        initWithSoundAssetUID:@"footstep3" 
        mixerDefinition:mixNode uid:@"footstep3"];

// Create the random node.
PHASERandomNodeDefinition* randomNode = 
    [[PHASERandomNodeDefinition alloc] initWithUID:@"randomNode"];

// Connect leaf nodes to the tree and set the weights. 
[randomNode addSubtree:footstep1 weight:@10];
[randomNode addSubtree:footstep2 weight:@5];
[randomNode addSubtree:footstep3 weight:@1];
```

## Topics

### Creating a Node

[`-  init`](/documentation/PHASE/PHASERandomNodeDefinition/init())

Creates a random node.

[`-  initWithIdentifier:`](/documentation/PHASE/PHASERandomNodeDefinition/init(identifier:))

Creates a random node with the name you specify.

### Adding Descendent Nodes

[`-  addSubtree:weight:`](/documentation/PHASE/PHASERandomNodeDefinition/addSubtree(_:weight:))

Adds a node tree that’s one of the random-selection options.

### Defining Selection Queue Length

[`uniqueSelectionQueueLength`](/documentation/PHASE/PHASERandomNodeDefinition/uniqueSelectionQueueLength)

The length of the unique selection queue.

## Relationships

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`Hashable`](/documentation/Swift/Hashable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`PHASESoundEventNodeDefinition`](/documentation/PHASE/PHASESoundEventNodeDefinition)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)