Jump to content

Python program for generating 5 digit random values


zak100

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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))

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.