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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:45:46+00:00 2026-05-26T10:45:46+00:00

The homework task sounds something like this Generate a random vector of 4*n uniform

  • 0

The homework task sounds something like this

Generate a random vector of 4*n uniform integer numbers ranging from 1
to 6, reshape this vector into an array with four rows and n columns,
representing the outcome of n throws with four dice….

I’ve come up with the following solution :

import numpy as np

num = 10
v = np.random.randint(1,7,(num,4))

So far so good, but I noticed that my generated array is in the 4 x n shape at the point of creation, and it has not been reshaped. I’ve considered the following as well :

v = np.random.randint(1,7,(4*num))
z = np.reshape(v,(num,-4))

Is there a difference between these two in terms of how v is built up? From a couple of print statements, they seem the same (in terms of shape) but I could not make sense of the documentation.

  • 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-26T10:45:46+00:00Added an answer on May 26, 2026 at 10:45 am

    Both ways are equivalent, although your homework task seems to require to create a 1D array first and reshape it to 2D afterwards.

    v = np.random.randint(1, 7, (num, 4))
    

    NumPy allocates a single memory block for num*4 integers, fills them with random values drawn uniformly from the interval [1, 7), then returns an array that interprets this 1D array as a “C-contiguous” 2D array with num rows and 4 columns*.

    v = np.random.randint(1, 7, 4 * num)
    z = np.reshape(v, (num, 4))
    

    This does the same thing, but more explicitly: v still is a 1D array, and z is a view on v, interpreting the same data as 2D array. This means that changing a value in v will also change it in z, since both arrays point to the same memory.

    z = np.reshape(v, (num, -4))
    

    The -4 does not make sense — array dimensions cannot be negative. NumPy does support one dimension to take the special value -1, which means “infer the dimension automatically from the size of the array”. It seems that it also accepts other negative numbers for the same purpose but that is an undocumented feature. That means it might as well be a bug and removed from future versions. Either reshape it explicitly to (num, 4) or use (num, -1) to let NumPy auto-discover the 4.

    Please also note that your assignment says “four rows and n columns”, whereas your arrays will have num rows and 4 columns.


    *) For example, the 1D array [ 1 2 3 4 5 6 7 8 ] is reshaped to:

    [ 1 2 3 4 ]
    [ 5 6 7 8 ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a basic homework assignment which looks like this: While input <> -1
This one is a case of not doing your homework.:-) Apart from dynamic loading
This is a homework task, but it's very simple. The task comes with a
This is not a homework. Just an interesting task :) Given a complete binary
This is not a homework, it's more of a challenge from our prof but
This is not a homework, but new task assign by superior. To create a
I am doing this homework task where it requires me to take in a
I have a prolog homework, which should work like this: singles([1,2,3,2,2,4,1], [3,4]). true Now
Working on a homework assignment and am stuck with this task: Create a loop
Say that I have simple command line application, that looks something like this #

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.