python常用随机函数及带权重随机实例
来源:互联网 发布:注册淘宝卖家要钱吗 编辑:程序博客网 时间:2024/05/18 02:57
# -*- coding: utf-8 -*-import randoma = 1b = 10step = 3seq = [-1, -2, -3, -4, -5, -6, -7, -8, -9, 0]k = 2# 0 <= n < 1.0print 'random', random.random() # b <= n(float) <= aprint 'uniform', random.uniform(a, b)# a <= n(int) <= bprint 'randint', random.randint(a, b)# 从指定范围内,按指定基数递增的集合中,获取一个随机数print 'randrange', random.randrange(a, b, step)# 从序列中获取一个随机元素print 'choice', random.choice(seq)# 从指定序列中随机获取指定长度的片断print 'sample', random.sample(seq, k)# 将一个列表中的元素顺序打乱random.shuffle(seq)print 'shuffle', seq# 带权重的随机def __in_which_part(n, w):for i, v in enumerate(w):if n < v:return ireturn len(w) - 1def weighting_choice(data, weightings):s = sum(weightings)w = [float(x)/s for x in weightings]t = 0for i, v in enumerate(w):t += vw[i] = tc = __in_which_part(random.random( ), w)try:return data[c]except IndexError:return data[-1]print 'weighting_choice', weighting_choice(['a', 'b'], [10, 90])
0 0
- python常用随机函数及带权重随机实例
- 带权重的随机算法
- 带权重的随机算法
- 带权重(>0)的随机选择
- C# 带权重的随机 与 不带权重的随机
- 带随机种子的javascript随机函数
- EXCEL随机函数实例
- Delphi常用随机函数
- Delphi常用随机函数
- Python 随机函数
- python sleep函数 随机
- Python 随机函数
- Python随机函数
- Python中的随机函数
- python生成随机函数
- python基础--随机函数
- Python随机函数
- python中的随机函数
- LeetCode Course Schedule II
- ios pan手势
- 获取Android操作系统源代码
- 15.6 CCF推荐的国际期刊
- sqlzoo练习答案--SUM and COUNT
- python常用随机函数及带权重随机实例
- 微特喜前端面试总结
- Java中的简单工厂模式
- 踏上学习java的路
- 【android_温故知新】android 数据存储
- ORA-00845: MEMORY_TARGET not supported on this system
- jsp绝对路径问题
- 重拾C++(一):引用高级
- 派生类的继承方式总结