Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:23:37+00:00 2026-06-17T22:23:37+00:00

In a two-dimensional array of integers (e.g. int[3,3] ), consisting of all 0 values,

  • 0

In a two-dimensional array of integers (e.g. int[3,3]), consisting of all 0 values, I’d like to set n random elements of the array to the value 1 as efficiently as possible. The problem that I am running into is that the first elements in the array are more likely to have a value of 1 than other elements later in the array.

Here is my code. In the example below I am attempting to set exactly 3 randomly-selected elements of the 3×3 array to 1.

int sum = 0;

private void MakeMatrix()
{
    for (int i = 0; i < 3; i++)
    {
        for (int k = 0; k < 3; k++)
        {
            int n = _r.Next(2);

            if (n != 1 && sum < 3)
            {
                matrix[i, k] = 1;
                sum++;
            }
            else
            {
                matrix[i, k] = 0;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:23:38+00:00Added an answer on June 17, 2026 at 10:23 pm

    You might try something like the following. First initialize your matrix to all 0 values, then run the code below. It will set three random values in the matrix to 1.

    int count = 0;
    while (count < 3)
    {
        int x = r.Next(0, 3);
        int y = r.Next(0, 3);
    
        if (matrix[x, y] != 1)
        {
            matrix[x, y] = 1;
            count++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when declaring the two dimensional array int random[height][width]; and then using it in a
I am making a program that inserts a two-dimensional array with random integers and
I have a two dimensional array of integers. I would like to write an
In my app, I'm storing Bitmap data in a two-dimensional integer array ( int[,]
I'm working with two-dimensional array-values that should be inserted into a ArrayList. But this
I'd like to read numbers from file into two dimensional array. File contents: line
I have two-dimensional array of integers. First index indicates the number of channels. The
How would one go about converting a list of integers to a two-dimensional array?
I'm trying to select for a two dimensional array of integers, and directing the
Design an algorithm , FindElement(a,p), where a is two-dimensional square array of positive integers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.