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

  • Home
  • SEARCH
  • 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 7412423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:32:15+00:00 2026-05-29T06:32:15+00:00

I have the foll0wing code to generate a matrix of 0’s and 1’s using

  • 0

I have the foll0wing code to generate a matrix of 0’s and 1’s using java.util.Random and a 2d array. I was wondering if there is a way to define the probability of the number of 0’s and 1’s occurring per row in the matrix. I want to have an equal probability of generating both 0’s and 1’s.

I want to convert this matlab equivalent code to generate a matrix into the java equivalent.

G= rand(10,20)<.5

Any ideas or suggestions?

Thanks,
Bhavya

  • 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-05-29T06:32:17+00:00Added an answer on May 29, 2026 at 6:32 am

    There is no predefined method for filling your array with numbers, but instead you need to do lop over each element and assign it the number. Something like this:

    Random random = new Random();
    
    for (int outerIndex = 0; outerIndex < matrix.length; outerIndex++) {
        for (int innerIndex = 0; innerIndex < matrix[outerIndex].length; innerIndex++) {
            if (random.nextDouble() < 0.5) {
                matrix[outerIndex][innerIndex] = 1;
            } else {
                matrix[outerIndex][innerIndex] = 0;
            }
        }
    }
    

    This assumes that the default random generator is good enough. Adjust the 0.5 if you want higher probability for 1s or 0s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to generate two random numbers var attackRoll = Math.floor((Math.random()*6)+1);
I have the following code to generate some spans using cakephp's helper <div id=numberRow>
I have the following code to generate a random password string: <?php $password =
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
I have the following code. The function is to generate a random ID of
I have the following C++ code that tried to generate a random number. The
I am trying to generate html using .enter method. I have the following code:
On my node.js server I have the following code: //Generate a token var token
I have the following code function generate_pdf() { $fdf_data_strings = $this->get_hash_for_pdf(); #$fdf_data_names = array('49a'
I have the following code to generate a table listing brand/model/submodel/style for a group

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.