2

I am trying to generate 5x5 list that has exactly 10 ones placed in a random locations in the 2D list.

I want to make the rest of the entries is zeroes. How can I make it?

import random def randomNumbers(): mylist=[random.randint(0, 1) for _ in range(5)] return mylist 

9 Answers 9

8

You can do this

from random import shuffle def randomNumbers(): l=[1 for _ in range(10)]+[0 for _ in range(15)] shuffle(l) lst=[] for i in range(0,25,5): lst.append(l[i:i+5]) return lst 
Sign up to request clarification or add additional context in comments.

Comments

1

One way to do it would be to start with a 2-Dimensional list filled with zeroes, and then pick ten distinct coordinates at which to insert a one:

from random import sample width = 5 height = 5 sample_size = 10 assert sample_size <= width * height matrix = [[0] * width for _ in range(height)] for x, y in sample([(x, y) for x in range(width) for y in range(height)], k=sample_size): matrix[y][x] = 1 for row in matrix: print(row) 

Output:

[0, 0, 0, 1, 1] [1, 0, 0, 1, 0] [0, 0, 1, 1, 1] [1, 1, 1, 0, 0] [0, 0, 0, 0, 0] >>> 

Comments

1

Try this maybe (using NumPy):

  1. Create a nxn matrix of zeros
  2. While sum of this matrix > 10, keep randomly selecting 2 indexes and set that value to 1
def get_output(shape, n): Z = np.zeros(shape) while np.sum(Z)<n: Z[np.random.randint(0,shape[0]),np.random.randint(0,shape[1])] = 1 return Z get_output((5,5), 10) 
array([[0., 0., 1., 1., 0.], [1., 0., 1., 0., 0.], [0., 0., 1., 0., 0.], [0., 1., 1., 1., 0.], [0., 1., 0., 1., 0.]]) 
get_output((3,3), 2) 
array([[1., 0., 0.], [0., 0., 0.], [1., 0., 0.]]) 

Comments

1

For matrix work, the usual preferred solution is numpy. Numpy has an array datatype that is much more flexible than 2D lists. However, here's one possible solution using Python lists only:

 import random li = [[0] * 5 for _ in range(5)] # make 2D list of zeros inds = random.sample(range(25), 10) # get 10 random linear indices for ind in inds: i, j = ind // 5, ind % 5 # convert the linear indices to 2D li[i][j] = 1 

Comments

1

This should work:

import numpy as np shape_1d = 5 shape_2d = 5 number_element = shape_1d * shape_2d number_of_ones = 10 xx = np.zeros((number_element,1)) idx = np.random.choice(number_element, number_of_ones) xx[idx] = 1 xx = xx.reshape((shape_1d, shape_2d)) xx = xx.tolist() 

Comments

0

This is the algorithm with nested loop, but I did not save the matrix inside list, you can try that out yourself.

import random for val in range(10): for val1 in range(10): print(random.randint(0, 1), end=' ') print("\n") 

Comments

0

Another solution using numpy is as follows:

import numpy as np import random N = 5 ones_N = 10 x = np.zeros((N,N)) indices = random.sample(range(N*N), ones_N) x.ravel()[indices] = 1 

You transfrom the 2D array to a 1D one, and then set the values at position indices to 1.

indices will be a ones_N elements array of values sampled from range(N*N).

Another option is to just shuffle 2D array that contain ones_N 1.

import numpy as np N = 5 ones_N = 10 x = np.zeros((N,N)) x.ravel()[:ones_N] = 1 np.random.shuffle(x.ravel()) 

Comments

0

First make a 1D list with the 0s and 1s, using random.sample to distribute the 1s randomly without picking the same index twice:

from random import sample pos_of_ones = sample(range(25), 10) list_1D = [1 if i in pos_of_ones else 0 for i in range(25)] 

Then to make this 2D, two choices:

  • With Numpy (recommended):
result = np.array(list_1D).reshape(5, 5) 
  • Just using lists:
result = [] for i in range(0, 25, 5): result.append(list_1D[i:i+5]) 

Comments

0

from random import randint

y=[]

for p in range(0, 20): #length of list will be 20

y.append([randint(0, 20),randint(50, 90)]) #1st list will have random numbers from 0 to 20 and 2nd list will have numbers from 50 to 90 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.