本系列不会对python语法,理论作详细说明;所以不是一个学习教材;详细查考Vamei 大神;通俗易懂;是一个很好(基础-中级-高级)的学习教程。而这里只是我一个学习python的某些专题的总结。
 
1. random()函数 

描述random() 方法返回随机生成的一个实数,它在[0,1)范围内。

语法:

import random
random.random();
注意:random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。 
实例演示:

>>> import random
>>> print random.random();
0.803119901575
>>> print random.random();
0.451592468747
2. randrange()函数
 描述: randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1。返回一个整数

 语法

import random
random.randrange ([start,] stop [,step])
#参数:
     start -- 指定范围内的开始值,包含在范围内
     stop -- 指定范围内的结束值,不包含在范围内。
     step -- 指定递增基数

实例演示

>>> print random.randrange(10);
4
>>> print random.randrange(5,10);
7
>>> print random.randrange(5,10,3);
5
>>> print random.randrange(5,10,3);
8
3.randint()函数
 描述:randint()方法将随机生成一个整数,它在[x,y]范围内 ;有点等价于randrange(x,y+1).
 语法

import random
random.randint(x,y)
#参数:
     x -- 指定范围内的开始值,包含在范围内
     y -- 指定范围内的结束值,包含在范围内。

实例演示

>>> print random.randrange(5,10);
9
>>> print random.randint(5,10);
6
4. uniform()函数
 描述:uniform() 方法将随机生成下一个实数,它在[x,y]范围内。返回一个浮点数 

 语法:

import random
random.uniform (x,y)
#参数:
     x -- 指定范围内的开始值,包含在范围内
     y -- 指定范围内的结束值,包含在范围内。

实例演示

>>> print random.uniform(5,10);
9.13282585434
>>> print random.uniform(9,10);
9.95958315062
5. choice()函数
描述:choice() 方法返回一个列表,元组或字符串的随机项。

语法

import random
random.choice(x)
#参数:
     x -- list,tuple,strings的一种

实例演示

>>> print random.choice(('a','be',5,'e'))
5
>>> print random.choice([10,2,6,5,85,'af'])
85
>>> print random.choice('i love python')
v

6. sample()函数

描述:sample()方法返回随机从列表,元组或字符串其中部分随机项 ;返回类型为元组类型

语法

import random
random.sample(x,n)
#参数:
     x -- list,tuple,strings的一种
     n -- 返回n个随机项

实例演示

>>> print random.sample('i love python',3)
[' ', 'e', 'i']
>>> print random.sample([10,20,50,23,'ab'],3)
[50, 'ab', 23]
>>> print random.sample((10,20,50,23,'ab'),3)
[50, 20, 'ab']
7. shuffle()函数

描述:shuffle() 方法将序列的所有元素随机排序。类似于洗牌

语法 :

import random
random.shuffle(x)
#参数:
     x -- list,tuple的一种;python2.x只支持list类型

实例演示

>>> list=['a','b','c','d','e'];
>>> random.shuffle(list);
>>> print list;
['c', 'd', 'a', 'e', 'b']

拓展:将元祖反转;实现reverse函数的效果

>>> list=['a','b','c','d','e'];
>>> list1=list[::-1]
>>> print list1
['e', 'd', 'c', 'b', 'a']

1. python中的随机函数的更多相关文章

  1. python中的随机函数random的用法示例

    python中的随机函数random的用法示例 一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据 ...

  2. python中的随机函数

    python--随机函数(random,uniform,randint,randrange,shuffle,sample) 本文转载自:[chamie] random() random()方法:返回随 ...

  3. [转]Python中的str与unicode处理方法

    早上被python的编码搞得抓耳挠腮,在搜资料的时候感觉这篇博文很不错,所以收藏在此. python2.x中处理中文,是一件头疼的事情.网上写这方面的文章,测次不齐,而且都会有点错误,所以在这里打算自 ...

  4. python中的Ellipsis

    ...在python中居然是个常量 print(...) # Ellipsis 看别人怎么装逼 https://www.keakon.net/2014/12/05/Python%E8%A3%85%E9 ...

  5. python中的默认参数

    https://eastlakeside.gitbooks.io/interpy-zh/content/Mutation/ 看下面的代码 def add_to(num, target=[]): tar ...

  6. Python中的类、对象、继承

    类 Python中,类的命名使用帕斯卡命名方式,即首字母大写. Python中定义类的方式如下: class 类名([父类名[,父类名[,...]]]): pass 省略父类名表示该类直接继承自obj ...

  7. python中的TypeError错误解决办法

    新手在学习python时候,会遇到很多的坑,下面来具体说说其中一个. 在使用python编写面向对象的程序时,新手可能遇到TypeError: this constructor takes no ar ...

  8. python中的迭代、生成器等等

    本人对编程语言实在是一窍不通啊...今天看了廖雪峰老师的关于迭代,迭代器,生成器,递归等等,word天,这都什么跟什么啊... 1.关于迭代 如果给定一个list或tuple,我们可以通过for循环来 ...

  9. python2.7高级编程 笔记二(Python中的描述符)

    Python中包含了许多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...

随机推荐

  1. frame与bounds的区别

    原来你M,frame.size和bounds.size不总是一样的 在UIViewController的- (void)willAnimateRotationToInterfaceOrientatio ...

  2. iOS 归档

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  3. 数据库报ORA-00600: 内部错误代码, 参数: [17059],并产生大量trace日志文件

    用户反馈数据库服务器磁盘空间使用耗尽. 登录服务器后查看,发现数据库产生大量的trace日志,并在alert日志中发现ora-600错误 alert日志信息: Fri Jul :: Errors ): ...

  4. Java基础之集合框架——使用堆栈Stack<>对象模拟发牌(TryDeal)

    控制台程序. public enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, A ...

  5. 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41844   Accepted: 12384 ...

  6. codeforces-Glass Carving(527C)std::set用法

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. codevs 1204 寻找子串位置

    http://codevs.cn/problem/1204/ 1204 寻找子串位置  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果 ...

  8. Ruby On Rails经典书籍下载地址

    Web开发敏捷之道-应用Rails进行敏捷Web开发   http://vdisk.weibo.com/s/t47M5Q3WInwc RUBY ON RAILS入门经典   http://downlo ...

  9. 关于非阻塞connect的若干细节性问题

    我们用man connection命令查看手册,如下:   EINPROGRESS The socket is nonblocking and the connection cannot be com ...

  10. yii框架中应用jquery表单验证插件

    效果图: 视图层: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...