0

Input:

import random
import numpy as np

gladList = np.empty((128,5))

for i in range(128):
    gladList[i]=(i,random.randint(1000,1500),random.randint(40,80),random.randint(0,100),random.randint(60,100))
    print(gladList[i])

expected output: [[ 49. 1169. 63. 70. 74.]] [[ 50. 1134. 80. 23. 70.]] . . .

error at output(at line 2): [[ 0. 1116. 72. 72. 69.]] [[1.000e+00 1.475e+03 6.800e+01 8.600e+01 6.900e+01]] [[ 2. 1443.
60. 53. 75.]] [[ 3. 1309. 80. 34. 91.]] [[ 4. 1020. 45. 32. 61.]]

I dont know why im getting this random numbers at line 2, if i run code again im getting even more errors in arrays. But first error is always at line 2(gladList[1]) its like a virus at my output.

6
  • Why do you think this is an error. It seems to be giving the random numbers provided by you for the range you provided. Commented Jun 12, 2020 at 10:12
  • Im expecting numbers like [1, 1000, 40, 0, 60] at output but getting instead [1.000e+00 1.475e+03 6.800e+01 8.600e+01 6.900e+01] where did they come from? Commented Jun 12, 2020 at 10:15
  • You mean you have scientific notation on the second line? Actually I'm seeing that too in all runs, precisely in the second line, not sure why. Commented Jun 12, 2020 at 10:15
  • Yeah im trying to make something like a game on Python to train coding. Its my generate random gladiator code, generating a gladiator with scientific numbers making me sad, i guess its something about numpy but im not sure. Commented Jun 12, 2020 at 10:20
  • Note that tese are not errors at all. Its just a different notation for those integers. What I'm not sure is of why the second row is always printed using that notation Commented Jun 12, 2020 at 10:20

2 Answers 2

1

If you think you are getting scientific notation, that is a problem for you, then you can do the following to remove it.

np.set_printoptions(suppress=True)

I hope this helps you, so whenever you will print the array you won't get exponential notation.

Sign up to request clarification or add additional context in comments.

Comments

0

np.random.randint lets us specify different integer ranges. Using that we can generate (128,4) points with one call. Here I'll use a smaller 10:

In [35]: arr = np.random.randint((1000,40,0,60),(1500,80,100,100),(10,4))                                       
In [36]: arr                                                                                                    
Out[36]: 
array([[1003,   42,   66,   74],
       [1421,   42,   54,   66],
       [1212,   46,   22,   61],
       [1048,   58,   52,   94],
       [1487,   50,   20,   76],
       [1249,   45,   80,   74],
       [1488,   45,   54,   74],
       [1105,   69,   53,   67],
       [1341,   47,   36,   84],
       [1161,   48,   81,   86]])

and adding the index column:

In [37]: arr = np.concatenate((np.arange(10)[:,None],arr),axis=1)                                               
In [38]: arr                                                                                                    
Out[38]: 
array([[   0, 1003,   42,   66,   74],
       [   1, 1421,   42,   54,   66],
       [   2, 1212,   46,   22,   61],
       [   3, 1048,   58,   52,   94],
       [   4, 1487,   50,   20,   76],
       [   5, 1249,   45,   80,   74],
       [   6, 1488,   45,   54,   74],
       [   7, 1105,   69,   53,   67],
       [   8, 1341,   47,   36,   84],
       [   9, 1161,   48,   81,   86]])

Doing the same row by row. Note create arr1 as integer dtype:

In [42]: arr1 = np.zeros((10,5),int)                                                                            
In [43]: for i in range(10): 
    ...:     arr1[i,0]=i 
    ...:     arr1[i,1:] = np.random.randint((1000,40,0,60),(1500,80,100,100),(1,4)) 
    ...:                                                                                                        
In [44]: arr1                                                                                                   
Out[44]: 
array([[   0, 1014,   62,   15,   77],
       [   1, 1046,   41,    6,   80],
       [   2, 1198,   67,   77,   67],
       [   3, 1306,   76,   25,   86],
       [   4, 1194,   60,   57,   62],
       [   5, 1068,   75,   32,   76],
       [   6, 1468,   74,   39,   67],
       [   7, 1268,   54,   47,   79],
       [   8, 1191,   75,    0,   71],
       [   9, 1164,   42,   27,   90]])

The same thing a float uses scientific notation because values range from 1 to 1000+:

In [45]: arr1.astype(float)                                                                                     
Out[45]: 
array([[0.000e+00, 1.014e+03, 6.200e+01, 1.500e+01, 7.700e+01],
       [1.000e+00, 1.046e+03, 4.100e+01, 6.000e+00, 8.000e+01],
         ...
       [9.000e+00, 1.164e+03, 4.200e+01, 2.700e+01, 9.000e+01]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.