python随机数模块的相关代码示例详解

开发 后端
一下的文章主要介绍的是python随机数模块的实际应用操作方案以及相关代码的详解,以下就是python随机数模块的相关内容的相关介绍。



python随机数模块

如果你对计算机语言python很了解的话,那么你对python中随机数模块是否了解呢?如果你对python随机数模块不是很了解的话,你可以浏览以下的文章对其进行了解,希望你会有所收获。

随机整数:

  1. >>> import random  
  2. >>> random.randint(0,99)  
  3. 21 

 

随机选取0到100间的偶数:

  1. >>> import random  
  2. >>> random.randrange(0, 101, 2)  
  3. 42 

 

随机浮点数:

  1. >>> import random  
  2. >>> random.random()   
  3. 0.85415370477785668  
  4. >>> random.uniform(1, 10)  
  5. 5.4221167969800881 

 

随机字符:

  1. >>> import random  
  2. >>> random.choice('abcdefg&#%^*f')  
  3. 'd' 

 

多个字符中选取特定数量的字符:

  1. >>> import random  
  2. random.sample('abcdefghij',3)   
  3. ['a', 'd', 'b'] 

多个字符中选取特定数量的字符组成新字符串:

  1. >>> import random  
  2. >>> import string  
  3. >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r  
  4. eplace(" ","")  
  5. 'fih' 

 

随机选取字符串:

  1. >>> import random  
  2. >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )  
  3. 'lemon' 

洗牌:

  1. >>> import random  
  2. >>> items = [1, 2, 3, 4, 5, 6]  
  3. >>> random.shuffle(items)  
  4. >>> items  
  5. [3, 2, 5, 6, 4, 1]  


python随机数模块的相关应用以及代码的介绍。

【编辑推荐】

  1. 用python pylint检查相关东西的操作方案详解
  2. Python入门的相对路径和绝对路径详解
  3. Python数据结构创建的具体应用方案详解
  4. Python语法的基本概念的阐述
  5. Python编程语言的实现内幕的相关介绍

 

责任编辑:佚名 来源: 博客园
相关推荐

2010-03-23 08:56:38

Python随机数模块

2010-03-23 09:47:38

Python随机数Python随机字符串

2010-03-19 14:44:30

Python模块级函数

2010-03-05 10:47:05

Python futu

2010-03-22 19:41:31

2010-03-22 15:18:27

2010-03-05 11:04:00

C调用Python函数

2010-03-22 17:53:50

Python字符Python字符串

2019-09-11 10:09:00

Java虚拟机算法

2010-02-01 17:02:53

C++产生随机数

2010-03-11 12:48:25

Python生成随机数

2010-03-17 14:42:09

Python 文件

2010-03-11 17:40:41

Python语言

2010-03-18 16:17:29

Python 变量类型

2010-06-13 10:59:38

MySQL数据库

2017-05-29 09:56:25

2010-03-19 11:04:20

python模块

2021-12-27 09:31:20

HashtableJava随机数

2011-07-08 10:32:11

Objective-C 随机数

2012-03-22 09:31:14

Java
点赞
收藏

51CTO技术栈公众号