random Decimal in python

后端 未结 7 1177
野趣味
野趣味 2020-12-31 08:24

How do I get a random decimal.Decimal instance? It appears that the random module only returns floats which are a pita to convert to Decimals.

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 09:03

    The random module has more to offer than "only returning floats", but anyway:

    from random import random
    from decimal import Decimal
    randdecimal = lambda: Decimal("%f" % random.random())
    

    Or did I miss something obvious in your question ?

提交回复
热议问题