简体   繁体   中英

Random even odd number generation in c#

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?

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

Edit: It is better to create the Random object once, so outside of the function (for numerous reason).

You can just use NextDouble() as follows:

public string Randomization(string a, string b)
{
    return new Random().NextDouble() < 0.5 ? a : b;
}

The solution will also work with Next(int maxValue) . However, if the method is being called enough times so that performance might be worth considering, NextDouble() will be a better option.

Here's the source code for both of these methods:

public virtual int Next(int maxValue) {
    if (maxValue<0) {
        throw new ArgumentOutOfRangeException("maxValue", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", "maxValue"));
    }
    Contract.EndContractBlock();
    return (int)(Sample()*maxValue);
}

public virtual double NextDouble() {
    return Sample();
}

Here's the reference: https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/random.cs#L181

One more point regarding performance. You should move the instantiation of the Random class outside of the Randomization() method.

You could also use a method that takes a string array. Then you don't have to create a new method if you have more strings.

Also, you should make the Random object a property or field. Otherwise, the method returns the same string if you call it often in a few milliseconds.

A method like that would be something like:

Random random = new Random();
public string Randomization(params string[] elements) {
      return elements[random.Next(elements.Length)];
}

For your case, because of the params keyword, usage would remain the same.

string result = Randomization("a", "b");

If you must have an 50% distribution across the two values ie 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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM