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 8095081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:00:24+00:00 2026-06-05T21:00:24+00:00

How can I make a random 4×4 array<array<int,4>,4> ? Each element of this 2D

  • 0

How can I make a random 4×4 array<array<int,4>,4>? Each element of this 2D array should be unique number from the interval from 0 to 15, inclusive.

Example:

6   7  5  4
10 11  12 15
1   3  2  8
9  14  0  13
  • 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-05T21:00:27+00:00Added an answer on June 5, 2026 at 9:00 pm

    Not a bad question. This would be my pick:

    How to generate subsequent array values, mix them and initialize 2D arrays with them?

    I extended my answer to include another (simple) solution to the problem using std::vector only, and std::vector + std::array (as asked by the O.P.).

      #include <vector>
      #include <array>
      #include <algorithm>
      using namespace std;
    
      // ...
      const int N = 4;  // we want to have 4x4 arrays for now
      // ...
    
      // C++ was tremendously simplified over the years in order to 
      // get a much more complicated language. This is what you can do ...
    
      // ...
      // First, generate a std::vector of shuffled numbers [0..15] over 16 elements
      vector<int> v;
      for(int i=0; i<N*N; i++) v.push_back(v.size()); // initialize w/index
      random_shuffle (v.begin(), v.end());            // shuffle element values
    
      // (Variant 1) std::array (C++11), row-wise copy of the vector data
      //  + will only work with newer (C++11 enabled) compiler versions
      array<array<int,N>,N> amat;  // there's no assign() member, so we have
      for(int i=0; i<N; i++)       // to copy each row through external template
         copy( v.begin()+i*N, v.begin()+(i+1)*N, amat[i].begin() );
      // done
      // ...
    

    In the for loop, we only do 4 iterations but have a total of 4×4 elements. Because each of the 4 matrix rows is 4 elements wide, we have to find a way how we take the correct 4 elements for each matrix row from our shuffeld 16-element 1D vector v: v.begin()+i*N ... v.begin()+(i+1)*N. If i is 0 (first iteration), we copy the four elements from v[0 * N] ... v[0+1 * N], this means v[0] .. v[4].

    This is a sequence where the last element v[4] is not included in the copy. This is also somehow an idiomatic pattern in C/C++ which is comparable to: for(i=START; i < END; i++) ....
    The END element is therefore beyond the range, not included.

    In the second iteration (i = 1), we have v[1 * N] ... v[1+1 * N], which is v[4] ... v[8]. You see the pattern?

      // ...
      // (Variant 2) std::vector (STL), row-wise copy of the vector data
      //  + should work with almost every c++ compiler
      vector<vector<int>> vmat(N);
      for(int i=0; i<N; i++) 
         vmat[i].assign( v.begin()+i*N,  v.begin()+(i+1)*N );
      // done
      // ...
      // TODO: now try to print all elements line 4x4 matrix
    

    Why is the shuffled order always the same? The C library uses a random number implementation that starts from the same seed number generating always the same sequence (which might be important for debugging). In order to get different shuffling, you’d have to re-initialize the random number generator at the program start once.

     ...
     srand((unsigned)time(NULL))
     ...
    

    For this, you need the C-library time-header (for time()) and most probably stdlib-header (for srand()):

     ...
     #include <ctime>
     #include <cstdlib>
     ...
    

    I deliberately tried to provide very simple solutions only. Therefore, no generators or C++11 lambdas seemed appropriate for this purpose.

    Regards

    rbo

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

Sidebar

Related Questions

How can I make a function which returns an array? I tried this const
How can I make a random number between something like 0.1 to 0.9 ?
I can make my program write a .dat file with an array of Node
I can make a variable's name from two variables' value: $a = 'tea'; $b
I can make batch or vbs file on windows and run. this can automate
I am new to Haskell and I wonder how/if I can make this code
I have 6 vectors which I want to plot. How I can make each
ReSharper sometimes hints that I can make some of my random utility methods in
Can jquery interact with XML like Flash do. I want to make random text
I have a legacy function that looks like this: int Random() const { return

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.