mardi 1 mars 2022

How to get random similar integers of the format like input?

let's say I have an decimal number like 20.65. I want to get x random decimal number that follows:

  1. Should have same number of digits.
  2. Should have same number of decimal places.
  3. Should be negative if the input integer is.
  4. There should be no repetition of any outputs or same as the input.

Example

I gave an input like:

Enter number : 50.26
Enter no of random numbers to be generated : 5

then it's output should be like:

12.36
69.74
58.39
54.56
94.45

Example 2:

Input:

Enter number : 5650.265
Enter no of random numbers to be generated : 5

then it's output should be like:

1652.326
6925.743
5844.394
5464.562
9448.454

Example 3:

Input:

Enter number : -456
Enter no of random numbers to be generated : 5

then it's output should be like:

-566
-492
-452
-151
-944

What I have tried :

from random import randint

n = float(input("Enter number : "))
x = int(input("Enter no of random integers to be generated : "))

min_choice = int('1'+''.join('0' for i in range(len(str(n))-1)))
max_choice = int(''.join('9' for i in range(len(str(n)))))

for i in range(x):
    print(randint(min_choice, max_choice))

which outputs as:

Enter number : 53.25
Enter no of random integers to be generated : 5
44864
29942
25832
20500
68083

So, as I can't the decimal places where I am struck.




Aucun commentaire:

Enregistrer un commentaire