0

Using the random class can I preselect numbers it can only use while just using Booleans?

For Example: Say I want a Random Number Between 0-9 But I want to only select from the numbers 1,4,6, and 8 if say The Booleans Named: "One", "Four", "Six", "Eight" are true.

    bool Zero = false;
    bool One = true;
    bool Two = false;
    bool Three = false;
    bool Four = true;
    bool Five = false;
    bool Six = true;
    bool Seven = false;
    bool Eight = true;
    bool Nine = false;

So It Would Output Something Like This: 18441861684168448418411864816

Is there a way to go about that using the "Random Class" that is below but swap out the values(1, 10) for only the true Booleans above or will I need to use something else? If so what do you recommend?

Random rnd = new Random();
int RandomizedNumber = rnd.Next(1, 10);  // creates a number between 1 and 9

I am using Visual Studios Windows Forms in C#.

Jaydon
  • 49
  • 5
  • 3
    So you want randomly 1 out of 4 numbres. So do `rnd.Next(0, 4)` and map the result to your actual values (if the result is `2` you return `6` etc) –  Jan 20 '21 at 12:19
  • 2
    Does this answer your question? [How to access random item in list?](https://stackoverflow.com/questions/2019417/how-to-access-random-item-in-list) – GSerg Jan 20 '21 at 12:21
  • 1
    Does this answer your question? [C# Select random element from List](https://stackoverflow.com/q/19318430/11683) – GSerg Jan 20 '21 at 12:21
  • @GSerg, 2 answers and ~200 point away from C# gold badge... – Drag and Drop Jan 20 '21 at 12:38

2 Answers2

3

A simple and optimized way to achieve this is to put the numbers you want to pick from in a list (easily modifiable, or in an array to have full speed), and select a random element from that list. This list could be populated based on the boolean values.

For example:

var list = new List<int> { 1, 4, 6, 8 };

Random rnd = new Random();

// Pick a random number from the list
int index = rnd.Next(list.Count);
int randomizedNumber = list[index];
Luca_Scorpion
  • 371
  • 4
  • 15
  • 1
    Next has a overload that allow you to avoid the 0 `Next (int maxValue)` => `rnd.Next(list.Count)` https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-5.0#System_Random_Next_System_Int32_ – Drag and Drop Jan 20 '21 at 12:32
  • @DragandDrop Good point, I've updated the question! – Luca_Scorpion Jan 20 '21 at 12:56
  • Okay so I am trying to implement your solution, but am having difficulty getting specific numbers into the list based on what has been selected on my form, now is there a way to add an item to the list . For example if say a button is pushed it will append the number 7 to the list of numbers to choose from? Is there also a way to clear the list or at least remove a specific number? – Jaydon Jan 20 '21 at 15:16
  • @Jaydon You can add or remove items from a list using `list.Add(item)` and `list.Remove(item)`, respectively. See: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=net-5.0 (here you can also find the other methods). Similarly, to clear the list, call `list.Clear()`. As to how that works best with your forms, that really depends on your setup, but this should give you a head start. – Luca_Scorpion Jan 20 '21 at 17:42
-1

If for some reason you want to keep that list of booleans you can go one step further before using the above answer from Luca_Scorpion.

I would create a Class with Booleans and Enum as a conversion. There might be an even simpler solution but at the moment I can't come up with one.

Then you can use LINQ to create a list of integers that you can use in the previous answer.

public class NoSelection
{
    public bool Zero { get; set; }
    public bool One { get; set; }
    public bool Two { get; set; }
    public bool Three { get; set; }
    public bool Four { get; set; }
    public bool Five { get; set; }
    public bool Six { get; set; }
    public bool Seven { get; set; }
    public bool Eight { get; set; }
    public bool Nine { get; set; }
}

public enum Numbers
{
    Zero,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine
}

public void GetRandomNumer()
{
    var NoToUse = new NoSelection
    {
        Zero = false,
        One = true,
        Two = false,
        Three = false,
        Four = true,
        Five = false,
        Six = true,
        Seven = false,
        Eight = true,
        Nine = false
     };

     var noList = NoToUse.GetType().GetProperties().Where(x => (bool) x.GetValue(NoToUse, null) == true)
            .Select(x => Convert.ToInt32((Numbers)System.Enum.Parse(typeof(Numbers), x.Name))).ToList();

    var rnd = new Random();
    var index = rnd.Next(noList.Count);
    int randomizedNumber = noList[index];
}
George
  • 32
  • 5