---
tags:
    - "Dice"
    - "Code"
    - "Password"
    - "Token"
    - "Nonce"
    - "Text"
---

# Random Characters

[Operations](../index.md) / [Generate](index.md) / **Random Characters**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

Generates a text of the specified length picking random characters from a set

## Parameters

| Name | Description |
| :--- | :---------- |
| Length | How many characters the resulting text has |
| Characters | The set of characters each position is picked from |

## Code

```cs
using System;
using System.Text;
using UnityEngine;
using VisualScript.Icons;
using Random = UnityEngine.Random;

namespace VisualScript.Runtime
{
    [Get]

    [Title("Random Characters")]
    [Category("Operations/Generate/Random Characters")]

    [Description("Generates a text of the specified length picking random characters from a set")]
    [Image(typeof(IconDice), ColorTheme.Type.Yellow, typeof(BadgeString))]

    [Parameter("Length", "How many characters the resulting text has")]
    [Parameter("Characters", "The set of characters each position is picked from")]

    [Keywords("Dice", "Code", "Password", "Token", "Nonce", "Text")]

    [Serializable]
    public class TypeStringRandomCharacters : TypeString
    {
        private const string CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        [SerializeField] private GetInteger m_Length = new GetInteger(8);
        [SerializeField] private GetString m_Characters = new GetString(CHARACTERS);

        [NonSerialized] private readonly StringBuilder m_Builder = new StringBuilder();

        public override string Get(Args args)
        {
            int length = this.m_Length.GetInt(args);
            string characters = this.m_Characters.Get(args);

            if (length <= 0 || string.IsNullOrEmpty(characters)) return string.Empty;

            this.m_Builder.Clear();
            for (int i = 0; i < length; ++i)
            {
                this.m_Builder.Append(characters[Random.Range(0, characters.Length)]);
            }

            return this.m_Builder.ToString();
        }

        public override string ToString()
        {
            return $"Random Text ({this.m_Length})";
        }

        protected override SubscriptionMode Subscription => SubscriptionMode.OnUpdate;
    }
}

```

