0

For some kid game, I have an array of size 10 of bool type. I got a random number between 1 to 10 and what I like to do, Is initialize only that number of cells in TRUE but in random order.

For example, If my random number is 4, I would like outputs like:

F T F F F T T F F T
T F F T T T F F F F

Is there an easy way to do it in C#?

Note, I know all the syntax (Init an array, work with random and all), I have a problem to find a short algorithm that does it.

4
  • 1
    I'd do this in two steps: 1) generate the array by populating the right number of items of each type in a simple way; 2) shuffle the array. There are lots of questions on Stack Overflow about shuffling. (In particular, search for Fisher-Yates shuffles.) Commented Sep 12, 2018 at 8:43
  • Random int, for loop, random index. job done Commented Sep 12, 2018 at 8:43
  • If your number is 4, you want 4 random indices into the array, making sure to skip the indices that give a position where you already set a cell to true. That looks like an algorithm to me. Commented Sep 12, 2018 at 8:45
  • @Daisy Shipton I used your suggestion on shuffling. It works great (I'm new so I think I can't upvote you) Commented Sep 12, 2018 at 9:04

3 Answers 3

3

Generate:

int length = 10;
int numberOfTrue = 4;

bool[] array = Enumerable
  .Range(0, length)
  .Select(index => index < numberOfTrue)
  .ToArray();  

And shuffle (e.g. with Fisher–Yates algorithm):

Random random = new Random();

...

for (int i = array.Length - 1; i >= 1; --i) {
  int from = random.Next(i + 1);

  var h = array[i];
  array[i] = array[from];
  array[from] = h;
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Just like in real life, create the number of trues you want, the number of falses that you need and then put them in a bag and shuffle them:

var random = new Random();
int numberOfTruths = 7;
int numberOfFalsehoods = 10 - numberOfTruths;

var truths = Enumerable.Repeat(true, numberOfTruths);
var falsehoods = Enumerable.Repeat(false, numberOfFalsehoods);

var arrayOfBools = truths.Concat(falsehoods).OrderBy(x => random.Next()).ToArray();

2 Comments

While I approve of the general approach, there are better ways of shuffling - there are lots of examples in other questions.
0

Another solution, if you don't want to use lists.

var rndBoolArray = new bool[10];

Random rnd = new Random();
int rndTrueCount = rnd.Next(1, 10); // Generate random number for the amount of true values in the array

for(int i = 0; i < rndTrueCount; i++)
{
    int rndIndex = rnd.Next(0, 9); // Generate random number for the index

    if(rndBoolArray[rndIndex] == false) // If the value at this position is not already true
        rndBoolArray[rndIndex] = true;
    else
        rndTrueCount++; // We have to do one more loop, if the value of the array at the random index was already true
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.