let's say I have an decimal number like 20.65
. I want to get x random decimal number that follows:
- Should have same number of digits.
- Should have same number of decimal places.
- Should be negative if the input integer is.
- 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.
CodePudding user response:
You can split it into two functions, one to generate a single number, and the other to collect the values making sure no duplicates are included:
import random
def gen_num(inp):
out = ""
if inp[0] == '-':
out = "-"
inp = inp[1:] # Skip sign
# The loop insures the generated numbers
# are of the same length as inp
for dig in inp:
if dig == ".": # Keeping decimal separator where it was
out = "."
else:
out = str(random.randint(1, 9))
return out
def gen_nums(inp, n):
out = set() # Sets can't contain duplicates
while len(out) < n:
num = gen_num(inp)
if num != inp: # Making sure no output number is same as input
out.add(num)
return out
if __name__ == "__main__":
inp = input("Enter number: ")
n = int(input("Enter no of random numbers to be generated: "))
for v in gen_nums(inp, n):
print(v)
Beware of casting the input to a float and back, you might be subject to floating point error.
Also be aware that there is a finite number of numbers that can be generated given the constraints (you can't generate 9
values from input 1
) and you might want to define what should happen in such case (what do you think would happen using the code above?).
CodePudding user response:
You could convert the input to a str and then use split
user_input = 123.321
input_parts = str(user_input).split('.') # This will return a list ["123", "321"]
Then you could get the lengths of each with len
left_side = len(input_parts[0])
right_side = len(input_parts[1])
Use those lenghts to generate appropriate length integers and join them.
left_num = str(random.randint(10**(left_side - 1),(10**left_side)-1))
right_num = str(random.randint(10**(right_side - 1),(10**right_side)-1))
Now you have 2 numbers of appropriate length. Just join them.
sides = [left_num, right_num]
merge = '.'.join(sides)
final_num = float(merge)