Python中random() 函数

一、Python random() 函数

    1、含义:
    random() 方法返回随机生成的一个实数,它在[0,1)范围内。
    2、语法:
    import random
    random.random()
    注意:random()是不能直接访问的,需要导入random模块,然后通过random静态对象调用该方法。
    3、实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import string

# 随机生成1到0之间的一个浮点数
print (random.random())
# 随机生成1到10的一个整数型随机数
print (random.randint(1, 10))
# 随机生成从0到100之间的间隔为2的随机整数
print (random.randrange(0, 100, 2))
# 随机生成1.0到5.0之间的随机浮点数,区间可以不是整数
print (random.uniform(1.0, 5.0))
# 随机选取一个在已定字符串中的字符
print (random.choice('todayistoday')) 
# 随机选取字符串
print (random.choice(['剪刀', '石头', '布']))
# 随机选取字符串中指定数量的字符:
print (random.sample('tomorrowistomorrow',5))
# 从a-zA-Z0-9生成指定数量的随机字符
print (''.join(random.sample(string.ascii_letters + string.digits, 5)))
# 随机选取指定数量的字符串组成新的字符串
print (''.join(random.sample(['a','b','c','d','e','f','g','h'], 5)))	
# 将序列a中的元素顺序打乱
a=[1,3,5,6,7] 	
random.shuffle(a)
print (a)

 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值