Jump to content

Python program for generating 5 digit random values

Featured Replies

Hi,

I am getting some problem with 'n'. Following is my code:

import random

class TestRandom:

    arr=[ ]
    def __init__(self, n):
        self.n = n
    def generate1000_5digit_RandomNum(self, n):
        for i in range(n):
            num = random.randint(10000, 99999)
            TestRandom.arr[i] = num
        for i in range(n):
            print(TestRandom.arr[i])
    def main(self, n):
        self.generate1000_5digit_RandomNum(n)

if __name__ == "__main__":
    objTestRandom = TestRandom(20)
    objTestRandom.main(20)

I am getting following error:
 

Quote

 

/home/zulfi/PycharmProjects/sort/venv/bin/python /home/zulfi/PycharmProjects/sort/20_5digitRandom.py
Traceback (most recent call last):
  File "/home/zulfi/PycharmProjects/sort/20_5digitRandom.py", line 19, in <module>
    objTestRandom.main(20)
  File "/home/zulfi/PycharmProjects/sort/20_5digitRandom.py", line 15, in main
    self.generate1000_5digit_RandomNum(n)
  File "/home/zulfi/PycharmProjects/sort/20_5digitRandom.py", line 11, in generate1000_5digit_RandomNum
    TestRandom.arr = num
IndexError: list assignment index out of range

Process finished with exit code 1

 

Somebody please guide me.

 

Zulfi.

8 hours ago, zak100 said:

Somebody please guide me.

It would help if you post the line numbers for the code when posting error messages referring to line numbers.

Hints*:
-What does "IndexError: list assignment index out of range" mean?
-Does the variable arr contain an element at index i?

*) Posted in Homework Help, no solution but hints will be given.

 

Edited by Ghideon

  • Author

Hi,

I am storing the element in the array.

Why index out of range?

I am creating an unsized array.

How to store elements in the array?

Please guide me.

 

Zulfi.

Edited by zak100

5 hours ago, zak100 said:

Why index out of range?

Because: (bold by me)

5 hours ago, zak100 said:

I am creating an unsized array.

If you try to access an element that does not exist, Pythons answer is index out of range. 

5 hours ago, zak100 said:

How to store elements in the array?

Maybe you could try the list* functions in Python? There are plenty of references and tutorials online.

 

 

*) Python supports lists, one of several available types of collections

  • Author

Hi,

 

Thanks for guidance. I am able to solve this problem.

 

God bless you.

 

Zulfi.

16 hours ago, zak100 said:

Thanks for guidance. I am able to solve this problem.

Thanks for feedback! To help any other member that may be interested: 

If this line

TestRandom.arr[i] = num

Is changed to this:

TestRandom.arr.append(num)

It will print random numbers

There may be requirements in the homework for writing the program as in OP but if the task is to get 20 random numbers it can be expressed more compact:

import random
for i in range(20): print(random.randint(10000, 99999))

 

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.