code_junkie

random Decimal in python

Nov 13th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. decimal.Decimal(random.randrange(10000))/100
  2.  
  3. >>> import random, decimal
  4. >>> decimal.Decimal(str(random.random()))
  5. Decimal('0.467474014342')
  6.  
  7. >>> import decimal
  8. >>> import random
  9. >>> def gen_random_decimal(i,d):
  10. ... return decimal.Decimal('%d.%d' % (random.randint(0,i),random.randint(0,d)))
  11.  
  12. ...
  13. >>> gen_random_decimal(9999,999999) #4 digits before, 6 after
  14. Decimal('4262.786648')
  15. >>> gen_random_decimal(9999,999999)
  16. Decimal('8623.79391')
  17. >>> gen_random_decimal(9999,999999)
  18. Decimal('7706.492775')
  19. >>> gen_random_decimal(99999999999,999999999999) #11 digits before, 12 after
  20. Decimal('35018421976.794013996282')
  21. >>>
  22.  
  23. from random import random
  24. from decimal import Decimal
  25. randdecimal = lambda: Decimal("%f" % random.random())
Add Comment
Please, Sign In to add comment