Hi, How can I add a number with the generated number which consists of 18 digits?

a = 100 + random.random(18) 

I want to get the result 100151215121512161518

  • Ok figures ... with Russian is bad - alex
  • Oh, got it. Then another question: do you want to get a number or string as a result? A random number will always be 18 digits, i.e. from 1000000000000000 to 999999999999999999? Or from 1 to 18 digits, i.e. from 0 to 999999999999999999? - Nick Volynkin
  • I need 100 + number Cator consists of 18 digits - alex

5 answers 5

http://ideone.com/tQSJMm

 from random import randint a = int("100" + str(randint(1e18, 1e19-1))) print(a) a = int(100e19) + randint(1e18, 1e19-1) print(a) 
  • random () takes no arguments (1 given) - alex
  • I assumed that your code works, but it is incorrect. If this is not the case, try using the ideas in the answer on the correct code. Or use another answer. - Qwertiy
  • @alex you wrote random.random(18) in the question, but now it doesn't work for you? - Nick Volynkin
  • @NickVolynkin, he wrote pseudocode at random. And when he was perceived as a working code and was slightly written in accordance with the minimum understanding of python, he decided to say that it did not work at all ... - Qwertiy
  • @NickVolynkin, I still fixed it. - Qwertiy
 >>> from random import randrange >>> 10**20 + randrange(10**18) 100202623310201395011 
  • Something I do not believe this answer ... randrange not randrange return too small a number? - Qwertiy
  • @Qwertiy Formula returns from 10**20 to 10**20+10**18-1 . Given that the author uses (broken) random.random(18) , the interpretation that '0'*18 contains 18 zeros (digits) is quite appropriate and the final formula in the answer is more logical. - jfs
  • Hmm .. And maybe so :) But in other answers he did not say that something was wrong :) - Qwertiy
 a = '100' + str(random.randint(1e18, 1e19-1)) 

It turns out to be a string . If you need a number , add int

 a = int('100' + str(random.randint(1e18, 1e19-1))) 
  • It turns out a string, not a number. - Qwertiy

For example, like this: a = int(str(100) + str(random.randint(100000000000000000, 999999999999999999)))

  • random () takes no arguments (1 given) - alex
  • @alex replace random.random(18) with random.randint(100000000000000000, 999999999999999999) - ragmon
  • And there is nothing simpler? - alex
  • @alex is nowhere easier - ragmon
  • We convert numbers into strings, concatenate them, then back into numbers. Yes, it seems to be easier) - Nick Volynkin
 >>> a = int(str(100)+"".join([str(random.randint(1,9)) for i in range(18)])) >>> a 100613877729784556545 
  • There are no zeros here. - Qwertiy
  • With zeros a = int(str(100)+"".join([str(random.randint(0,9)) for i in range(18)])) - Alexander Loev