-3

how to make a number generator on loops and limit each repetition to a maximum of 100, so that the number of numbers generated is 100 numbers.

Example of the resulting number:

EE-02100500004848486527

EE-02100500004848486432

up to 100 numbers.

so here I want the last 4 numbers to be randomized

this is my code

for x in range(100):
    print("EE-02100500004848486527")
  • 1
    `print(*(f"EE-0210050000484848{randrange(1000, 10000)}" for _ in range(100)), sep="\n")` [`random.randrange()`](https://docs.python.org/3/library/random.html#random.randrange) – Olvin Roght Aug 07 '21 at 20:46
  • bro code that you write doesn't work – Nancy Savchenko Aug 07 '21 at 21:05
  • 1
    Bro(Sis), code I wrote [is working](https://tio.run/##K6gsycjPM7YoKPr/P60oP1ehKDEvBUhl5hbkF5WAeUCcnsrFVVCUmVeioaWRpuTqqmtgZGhgYGoABCYWIFgNV6hhoKNgCJLQtDIwSalVUkjLL1KIV8jMU4BIA@U0NXUUilMLbJVi8pQ0//8HAA) for sure. I guess you've forgotten `from random import randrange`. Btw, in code from link I've used some advance formatting `f"EE-0210050000484848{randrange(0, 10000):04d}"` – Olvin Roght Aug 07 '21 at 21:08
  • Ah my mistake, now the code is already running, oh yeah how to save the code into the txt file? – Nancy Savchenko Aug 07 '21 at 21:13
  • 1
    Use search, there're thousands of questions with accepted answers which could help you. – Olvin Roght Aug 07 '21 at 21:13
  • Does this answer your question? [How to generate a random number with a specific amount of digits?](https://stackoverflow.com/q/2673385/6045800) – Tomerikoo Aug 07 '21 at 21:17

2 Answers2

1

You can use numpy's random generator or the standard random library below an quick example with numpy

import numpy as np
for i in range(100): 
  print("EE-0210050000484848{:04d}".format(np.random.randint(0,9999)))    

to use the standard random library simply use import random and then random.randint() instead of the numpy function

braulio
  • 543
  • 2
  • 13
  • It's also not necessary to import numpy for something that can be easily done with a built-in library... – Tomerikoo Aug 07 '21 at 21:18
1

You can use this:

import random
with open("your_file.txt", "w") as f:
    for _ in range(100):
        f.write(f"EE-0210050000484848{random.randint(0, 9999):04d}\n")
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160