python random 随机选择操作

# -*- coding:utf-8 -*-
import random
arr = [‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘]
#生成(0.0, 1.0)的随机数
print random.random()
#0.133648715391

# 生成随机浮点数 0<N<100
print random.uniform(0,100)
#10.535881824

#生成随机整数 0<N<100
print random.randint(0,100)  

#随机生成一个0-100内3的倍数
print random.randrange(0,100,3)

#29
#随机选择一个元素
print random.choice(‘1234567890‘)
#6
print random.choice(arr)
#B

#随机选择指定长度不重复元素
print random.sample(‘1234567890‘,4)
#[‘3‘, ‘8‘, ‘1‘, ‘9‘]
print random.sample([‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘],4)
#[‘C‘, ‘B‘, ‘A‘, ‘D‘]

#打乱列表
random.shuffle(arr)
print arr
#[‘E‘, ‘B‘, ‘D‘, ‘A‘, ‘C‘, ‘F‘]

原文地址:https://www.cnblogs.com/caiyishuai/p/9650639.html

时间: 2024-08-30 10:21:26

python random 随机选择操作的相关文章

python random 从集合中随机选择元素

使用python random模块的choice方法随机选择某个元素 from random import choice foo = ['a', 'b', 'c', 'd', 'e'] print (choice(foo)) 使用python random模块的sample函数从列表中随机选择一组元素 list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回 pri

python的内置模块random随机模块方法详解以及使用案例(五位数随机验证码的实现)

1.random(self): Get the next random number in the range [0.0, 1.0) 取0到1直接的随机浮点数 import random print(random.random()) C:\python35\python3.exe D:/pyproject/day21模块/random随机模块.py 0.3105503800442595 2.randint(self, a, b) Return random integer in range [a

在python中实现随机选择

想从一个序列中随机抽取若干元素,或者想生成几个随机数. random 模块有大量的函数用来产生随机数和随机选择元素.比如,要想从一个序列中随机的抽取一个元素,可以使用random.choice() : >>> import random >>> values = [1, 2, 3, 4, 5, 6] >>> random.choice(values) 2 >>> random.choice(values) 3 >>>

Python随机选择Maya场景元素

之前在公司参与的一个与国外合作的项目中,需要动态的随机选取场景中的一些物体,当时是用Houdini的节点+Hscript 解决的: 今天用简洁优雅的Python在Maya中写了一个类似的效果,代码如下: import maya.cmds as mc import random def selTest(): mc.select(allDagObjects=1) sel = mc.ls(sl=True) selSize = len(sel) #print sel rand = random.samp

Python常用模块——random随机模块

Python常用模块--random随机模块 程序中有很多地方都需要用到随机字符,比如登录网站的随机验证码,通过random模块可以很容易生成随机字符串. >>> random.randrange(1,10) #返回1-10之间的一个随机数,不包括10 >>> random.randint(1,10) #返回1-10之间的一个随机数,包括10 >>> random.randrange(0, 100, 2) #随机选取0到100间的偶数,即步长为2 &g

Python基础-----random随机模块(验证码)

random随机模块的用法及功能 import random print(random.random())#(0,1)----获取0-1中的一个float print(random.randint(1,3)) #[1,3]取范围内的一个整数 print(random.randrange(1,3)) #[1,3)取范围内的一个整数 print(random.choice([1,'23',[4,5]]))#23 随机获取可迭代对象中的一个元素 print(random.sample([1,'23',

【工作中的Python】随机点名小脚本

背景:项目组每周的例会中,有一项固定内容就是技术分享,可以是与工作相关或无关的任何技术主题.进行技术分享讲解的同学是随机抽签的.由此做了一个Python的小脚本用于抽取姓名. 脚本内容如下: #!/usr/bin/python import os import sys import tty, termios import random name_list = ["member_1","member_2","member_3"] input = '

[Python] Python 学习 - 可视化数据操作(一)

Python 学习 - 可视化数据操作(一) GitHub:https://github.com/liqingwen2015/my_data_view 目录 折线图 散点图 随机漫步 骰子点数概率 文件目录 折线图 cube_squares.py import matplotlib.pyplot as plt x_values=list(range(1, 5000)) y_values=[pow(x, 3) for x in x_values] plt.scatter(x_values, y_v

python进阶--文件读写操作

Python读写文件 1. open 使用open打开文件后一定要记得调用 文件对象的close()方法.比如可以用try --finally语句来确保最后能关闭文件. >>>f1 = open('thisfile.txt') >>>try: f1.read() finally: f1.close() 2. 读文件(read,readline,readlines) ①读文本文件 input = open('data','r') input.read() ②读二进制文件