我在Coding Horror上读过一篇关于各种shuffle算法的文章.我已经看到人们已经在某个地方对列表进行了洗牌:
var r = new Random();
var shuffled = ordered.OrderBy(x => r.Next());
Run Code Online (Sandbox Code Playgroud)
这是一个很好的shuffle算法吗?它是如何工作的?这样做是否可以接受?
使用.NET随机化字符串数组的最佳方法是什么?我的数组包含大约500个字符串,我想Array用随机顺序创建一个具有相同字符串的新字符串.
请在答案中加入C#示例.
我一直在做一些休闲度假计算.我的迷你项目是对意大利"tomboli"游戏的模拟.一个关键的构建模块是对以下过程的模拟;
游戏由一个男人控制,一袋90个大理石,编号为1到90.他从包里随机抽出弹珠,每次都给玩家打出大理石号码.
经过一番思考后,我为这个构建块编写了以下代码;
// NBR marbles, numbered 1...NBR are in a bag. Simulate randomly
// pulling them from the bag, one by one, until the bag is empty
void bag( int random_sequence[NBR] )
{
int i;
// Store each marble as it is pulled out
int *store = random_sequence;
// Array of marbles still in the bag
int not_yet_pulled[NBR];
for( i=0; i<NBR; i++ )
not_yet_pulled[i] = i+1; // eg NBR=90; 1,2,3 ... 90
// Loop pulling marbles from the bag, …Run Code Online (Sandbox Code Playgroud)