本篇文章给大家谈谈Python-为什么random.shuffle返回None?,以及python中random.shuffle的知识点,同时本文还将给你拓展c–random_shuffle不是真的随
本篇文章给大家谈谈Python-为什么random.shuffle返回None?,以及python中random.shuffle的知识点,同时本文还将给你拓展c – random_shuffle不是真的随机、numpy.random.shuffle()与numpy.random.permutation()的区别、PHP函数shuffle取数组若干个随机元素的方法分析 shuffle学园 shuffle怎么下歌 shuffle memorie、python random.shuffle的随机性等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- Python-为什么random.shuffle返回None?(python中random.shuffle)
- c – random_shuffle不是真的随机
- numpy.random.shuffle()与numpy.random.permutation()的区别
- PHP函数shuffle取数组若干个随机元素的方法分析 shuffle学园 shuffle怎么下歌 shuffle memorie
- python random.shuffle的随机性
Python-为什么random.shuffle返回None?(python中random.shuffle)
为什么要用Python random.shuffle
返回None
?
>>> x = [''foo'',''bar'',''black'',''sheep'']>>> from random import shuffle>>> print shuffle(x)None
我如何获得改组后的值而不是None
?
答案1
小编典典random.shuffle()
更改x列表到位。
就地更改结构的Python API
方法通常返回None,而不是修改后的数据结构。
如果要基于现有列表创建新的随机混排列表,并按顺序保留现有列表,则可以使用random.sample()
输入的完整长度:
x = [''foo'', ''bar'', ''black'', ''sheep'']random.sample(x, len(x))
你还可以将sorted()with random.random()
用于排序键:
shuffled = sorted(x, key=lambda k: random.random())
但这会调用排序(O(NlogN)
操作),而采样到输入长度仅需要O(N)操作(与random.shuffle()
所使用的过程相同,即从收缩池中换出随机值)。
演示:
>>> import random>>> x = [''foo'', ''bar'', ''black'', ''sheep'']>>> random.sample(x, len(x))[''bar'', ''sheep'', ''black'', ''foo'']>>> sorted(x, key=lambda k: random.random())[''sheep'', ''foo'', ''black'', ''bar'']>>> x[''foo'', ''bar'', ''black'', ''sheep'']
c – random_shuffle不是真的随机
#include <algorithm>vector <Card> deck;//some code to add cards to the deck hererandom_shuffle ( deck.begin(),deck.end() );
运行时,甲板的内容会混合起来,但是当我重新启动程序时,会保留这个混合顺序.
我错过了什么?如何让它真正随机?
解决方法
#include <algorithm>#include <cstdlib>...std::srand(std::time(0));vector <Card> deck;//some code to add cards to the deck hererandom_shuffle ( deck.begin(),deck.end() );
从以上链接注意:
Generally speaking,the pseudo-random number generator should only be seeded once,before any calls to rand(),and the start of the program. It should not be repeatedly seeded,or reseeded every time you wish to generate a new batch of pseudo-random numbers.
numpy.random.shuffle()与numpy.random.permutation()的区别
参考API:https://docs.scipy.org/doc/numpy/reference/routines.random.html
1. numpy.random.shuffle()
API中关于该函数是这样描述的:
Modify a sequence in-place by shuffling its contents. This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.
也就是说,numpy.random,shuffle(x)是进行原地洗牌,直接改变x的值,而无返回值。对于多维度的array来说,只对第一维进行洗牌,比如一个 $ 3 \times 3 $ 的array,只对行之间进行洗牌,而行内内容保持不变。 例子:
2. numpy.random.permutation()
API中关于该函数是这样描述的:
Randomly permute a sequence, or return a permuted range. If x is a multi-dimensional array, it is only shuffled along its first index.
也就是说,numpy.random,permutation(x)是返回一个被洗牌过的array,而x不变。对于多维度的array来说,只对第一维进行洗牌,比如一个 $ 3 \times 3 $ 的array,只对行之间进行洗牌,而行内内容保持不变。 例子:
PHP函数shuffle取数组若干个随机元素的方法分析 shuffle学园 shuffle怎么下歌 shuffle memorie
本文实例讲述了php函数shuffle()取数组若干个随机元素的方法。分享给大家供大家参考,具体如下:
有时候我们需要取数组中若干个随机元素(比如做随机推荐功能),那么PHP要如何实现呢?一个比较简单的解决方法是用PHP自带的shuffle()函数。下面举一个简单的例子:
$data[] = array( "name" => "本站", "rank" => "40" ); $data[] = array( "name" => "博客园", "rank" => "50" ); $data[] = array( "name" => "CSDN", "rank" => "60" ); $data[] = array( "name" => "ITEYE", "rank" => "50" ); shuffle($data); $i = 0; foreach($data as $key =>$value ){ if($i ''; } $i++; }
shuffle()
shuffle() 函数把数组中的元素按随机顺序重新排列。若成功,则返回 TRUE,否则返回 FALSE。本函数为数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序。
如果传入的是关联数组,你会发现关联数组的键名将丢失。顺便这里说一下解决方法:
立即学习“PHP免费学习笔记(深入)”;
// 打乱关联数组的排序 function shuffle_assoc($array) { $randomized_keys = array_rand($array, count($array)); foreach($randomized_keys as $current_key) { $output[$current_key] = $array[$current_key]; } return $output; }
另外,php还提供了从数组中随机抽取值的函数:array_rand(),其调用格式如下:array_rand(数组,抽取元素的个数); 当然要实现对二维数组的支持还需要封装一下才能使用。但前面的程序就很好支持二维数组。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
以上就介绍了PHP函数shuffle取数组若干个随机元素的方法分析,包括了shuffle,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
python random.shuffle的随机性
random.shuffle(x[,random])
Shuffle the sequence
x
in place. The optional argument random is a 0-argument function returning a random float in[0.0,1.0)
; by default,this is the functionrandom()
.Note that for even rather small
len(x)
,the total number of permutations ofx
is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.
如果我想重复获得[‘a’…’k’]的随机排列,似乎随机播放不会给我随机性.我的理解是对的吗?
谢谢!
解决方法
今天关于Python-为什么random.shuffle返回None?和python中random.shuffle的讲解已经结束,谢谢您的阅读,如果想了解更多关于c – random_shuffle不是真的随机、numpy.random.shuffle()与numpy.random.permutation()的区别、PHP函数shuffle取数组若干个随机元素的方法分析 shuffle学园 shuffle怎么下歌 shuffle memorie、python random.shuffle的随机性的相关知识,请在本站搜索。
本文标签: