Home > Software design >  How to randomize options/statements?
How to randomize options/statements?

Time:02-20

So I want to figure out how to randomize statements. So for example you guys know how true and false statements can be the deciding factor of an output? For example:

int time = 22;
if (time < 10) 
{
  Console.WriteLine("Good morning.");
} 
else if (time < 20) 
{
  Console.WriteLine("Good day.");
} 
else 
{
  Console.WriteLine("Good evening.");
}

Now using that same example, how would I make so instead of relying on if the time is less than 10 then giving the WriteLine output, it would choose from the 3 chooses and randomly pick one to write in the output?

CodePudding user response:

It's as simple as this:

[ThreadStatic]
private static Random _random = new Random();

private void YourMethod()
{
    var messages = new[] { "Good morning.", "Good day.", "Good evening." };
    Console.WriteLine(messages[_random.Next(messages.Length)]);
}

The _random variable is declared only once per thread making this code reliable and thread-safe. You don't have to worry too much about why, but this is a good habit to get into.


Alternatively, this works:

var messages = new[] { "Good morning.", "Good day.", "Good evening." };
Console.WriteLine(messages[Random.Shared.Next(messages.Length)]);

CodePudding user response:

There are likely hundreds of valid answers but here are two simple solutions to get you started:

Random index from array working example

// Array of messages
var messages = new [] {
    "Good morning.",
    "Good afternoon.",
    "Good evening."
};

// Random number generator
var randomNumbers = new Random();

// Generate random number within range of 0 to number of messages in array
var randomIndex = randomNumbers.Next(0, messages.Length);

// Choose message at index of random number
var messageAtIndex = messages[randomIndex];

// Display random message
Console.WriteLine(messageAtIndex);  

First from random sorted list (working example)

// Array of messages
var messages = new List<string> {
    "Good morning.",
    "Good afternoon.",
    "Good evening."
};

// Random number generator
var randomNumbers = new Random();

// Sort list by random numbers
var randomSortedMessages = messages.OrderBy(x => randomNumbers.Next());

// Choose first message from randomly sorted list
var firstMessage = randomSortedMessages.First();

// Display random message
Console.WriteLine(firstMessage);    

Note that these are not the most efficient rather easy to understand solutions with hopes of triggering your creativity.

CodePudding user response:

namespace TestSo;

internal class TestSo
{
    private static void Main(string[] args)
    {
        var rnd = new Random();
        var no = rnd.Next(1, 4); // A random number from 1 to 3
        switch (no)
        {
            case 1:
                Console.WriteLine("Good morning.");
                break;
            case 2:
                Console.WriteLine("Good day.");
                break;
            case 3:
                Console.WriteLine("Good evening.");
                break;
        }
    }
}

See this ref.

  •  Tags:  
  • c#
  • Related