аватар question@mail.ru · 01.01.1970 03:00

Filling the array with random values

   IMPort  RANDOM  import  timeli = [] tmp =  100000    for  i  in   range  ( 0 ,  100000 ):   if  tmp! =  0 :  x1 = radom.randint ( 0 , tmp)   Li.ppend (x1)  tmp = x1  else :  break  n =  1  start_time = Time.clock ()   when  n & lt;  len  (li):   for  i  in   range  ( Len  (LI) -n):   if  li [i] & gt; li [i+]:  li [i], li [i+ 1 ] = li [i+ 1 ], li [i]  n+=  1    print  (li)   print  ( "" {: g} seconds ""  format  (time.clock () - start_time))     

you need to fill in 100,000 array values ​​(and sort with the bubble), but it is lame a little with random

аватар answer@mail.ru · 01.01.1970 03:00

:

   import  radomitems = [radom.randint ( 0 , )  for  i  in   range  ( 100,000 )]   print  ( len  (items)) # 100000      

if only unique numbers in the indicated range, then you can simply generate All numbers from the range and mix them:

   import  radomitems =  list  ( range  ( 100000 ))   print  (items [:  5 ])) # [0, 2, 2, 3, 4]  random.shuffle (items)   print  (items [:  5 ])    import  numpyitems = numpy.random.randint ( 100000 , Size = )   print  ( len  (items), items [:  5 ]) # 100000 [73846 49707 18846 73887 43349]      

this function generates the indicated number of numbers, in the specified random range, but there is no guarantee of uniqueness numbers.


maxu proposed the generation of unique random numbers using numpy.random.choice :

   import  numpyitems = numpy.random.choice ( 100000 ,  100000 , replace =  false )   print  ( len  (items), items [:  5 ]) # 100000 [94792 79537 9678 66784 92049]                                        

Latest

Similar