Question: I have a function with two arguments say a and b. So basically, whenever I call this method 50% times it should return a and 50% times it should return b.

```
public string Randomization(string a, string b)

```

I thought of doing this using random number generation and if number generated is even, return a and if odd, return b. How could I achieve this or is there any other simple way ?

Answer: 
A simple random between 0 and 1 would do it.

```
private Random _rnd = new();
public string Randomization(string a, string b) {
    if (_rnd.Next(2) == 0) {
        return a;
    }
    return b;
}

```

[Random.Next](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?redirectedfrom=MSDN&view=net-5.0#System_Random_Next_System_Int32_System_Int32_)

Edit: It is better to create the Random object once, so outside of the function (for numerous reason).
If you must have an 50% distribution across the two values i.e if `a` was returned before, next time it should be `b`, you may consider the below logic.

I have used lock to support thread safety. You can remove them if there is no multi threading requirement. I have used a singleton pattern instead of static variable. Based on your need, you may simplify the code to use static variables instead of singleton.

The first determination is done by the checking the second the object is creating is odd or even.

```
using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Singleton.Instance.Randomization("one", "two"));
        Console.WriteLine(Singleton.Instance.Randomization("one", "two"));
        Console.WriteLine(Singleton.Instance.Randomization("one", "two"));
        Console.WriteLine(Singleton.Instance.Randomization("one", "two"));
        Console.WriteLine(Singleton.Instance.Randomization("one", "two"));
        Console.WriteLine(Singleton.Instance.Randomization("one", "two"));
    }
}

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());
    public static Singleton Instance
    {
        get
        {
            return lazy.Value;
        }
    }

    private static readonly object _lock = new Object();
    private bool _counter;
    private Singleton()
    {
        this._counter = DateTime.Now.Second % 2 == 0;
    }

    public string Randomization(string a, string b)
    {
        lock (_lock)
        {
            _counter = !_counter;
            return _counter ? a : b;
        }
    }
}

```