Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:52:03+00:00 2026-06-15T11:52:03+00:00

I need to write a function which will randomize some words of my string.

  • 0

I need to write a function which will randomize some words of my string. For example:

[Hello|Hi] guys. This is my [code|string]

The function should return:

Hello guys. This is my code

or

Hi guys. This is my string

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:52:04+00:00Added an answer on June 15, 2026 at 11:52 am

    You can get a random number generator like this:

    var rand = new Random();

    As for parsing your string and getting all of the options, I suggest you look into System.Text.RegularExpressions

    The other answers so far have just shown how you can get a random string if you already have one or two options for the different placeholders. Those are fine, but pretty boring and tedious to write out. It’s much better to write a parser that can take a random string “template” like the OP gave, and use that to generate the random strings.

    Here is a quick one I put together:

    using System;
    using System.Text.RegularExpressions;
    
    namespace StackOverLoadTest {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            var s = new RandomString("[Hey|Hi] guys. [I|You|We|He|She] should [walk] to the [park|field|farm] sometime [today|tomorrow|next week].");
            for (int i = 0; i < 10; i++)
                Console.WriteLine(s);
        }
    }
    
    public class RandomString {
        private Random _rnd = new Random();
        private static Regex _rex = new Regex(@"\[ ( \|?  (?<option>[^]|]+) )+ \]", System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
        string _template;
        public RandomString(string template) {
            _template = template;
        }
        public override string ToString() {
            return _rex.Replace(_template, GetRandomOption);
        }
        public string GetRandomOption(Match m) {
            var options = m.Groups["option"].Captures;
            int choice = _rnd.Next(0, options.Count);
            return options[choice].Value;
        }
    }
    }
    

    As you can see, you use create a new RandomString object with the template. Then simply call the ToString() function as many time as you want, and each time you get a new random permutation of the options.

    You can use any number of placeholders with any number of options (except 0).
    The string template I used in this example was:


    "[Hey|Hi] guys. [I|You|We|He|She] should [walk] to the [park|field|farm] sometime [today|tomorrow|next week]."

    Running the code above, I got the following results:



    Hey guys. I should walk to the park sometime today.
    Hi guys. We should walk to the farm sometime today.
    Hi guys. He should walk to the field sometime next week.
    Hey guys. You should walk to the park sometime next week.
    Hi guys. She should walk to the farm sometime next week.
    Hey guys. We should walk to the field sometime tomorrow.
    Hi guys. I should walk to the farm sometime today.
    Hey guys. He should walk to the field sometime tomorrow.
    Hi guys. You should walk to the park sometime next week.
    Hi guys. I should walk to the farm sometime today.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a TSQL user defined function which will accept a string
I need some help in getting this right, problem Write a function which takes
I need to write a delegate function that can 'wrap' some while/try/catch code around
This is my code all I need to do is call a function which
I need to write a Mysql function which will check for the feature entry
I need to write a function which will detect if the input contains at
I need to write a templated function replace_all in C++ which will take a
Problem: I need to write a function which returns a value for a input
I need to write a split function in JavaScript that splits a string into
i'm need to write a function that will flip all the characters of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.