简体   繁体   中英

How to create a list with random integers

I am trying to create a random list of 1 million numbers ranging between 1-100. I have found how to create a single random number but not to create a list of them. In addition, I would prefer to use the numpy uniform function but the solution doesn't have to use this.

For a big array, you'd better use numpy

 import numpy as np
 X = np.random.randint(1, 101, size=10**6)

Try this simpler one :

from random import randint

my_list = [randint(1, 100) for i in range(1000000)]

The function

np.random.rand(Dim1,Dim2)

will create an array for the given dimensions with values selected from the uniform distribution [0,1]. You can modify this to randomArray = 1 + 99 * np.random.rand(Dim1,Dim2) to have the array with numbers ranging between 1-100.

use this as simple as that...don't make your program more complex in code and in efficiency too..

from random import randint
l=[randint(1,100) for i in range(0,1000000)]
  from random import *
  mylist = []
  for i in range(0, 100):
        f = randint(0, 100)
        mylist.insert(i, f)

This code will give 100 random elements to your empty list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM