shuffle

C#: Is using Random and OrderBy a good shuffle algorithm?

I have read an article about various shuffle algorithms over at Coding Horror. I have seen that somewhere people have done this to shuffle a list: var r = new Random(); var shuffled = ordered.OrderBy(x => r.Next()); Is this a good shuffle algorithm? How does it work exactly? Is it an acceptable way of doing this? ...

How do I shuffle two arrays in exactly the same way in Perl?

Does anyone know how to shuffle two arrays randomly in exactly the same way in Perl? For example, say I have these two arrays: Before shuffling: array 1: 1, 2, 3, 4, 5 array 2: a, b, c, d, e After shuffling: array 1: 2, 4, 5, 3, 1 array 2: b, d, e, c, a So every element in each array is bound to its equivalent element. ...

Sorting TListbox -- Highs and Lows

Okay, I have a TListBox that on occasion may be called upon to show 43,000 lines! I know, this hardly ever makes any sense, but there it is. Now here's the current problem: Using the built-in Sort method, with its Compare callback function, takes nearly forever, like many minutes. So I extract the strings out of the listbox into a p...

How to permute array into a given order with O(1) auxiliary space?

How do I implement the following OrderElements function? char chars[] = {'a', 'b', 'c', 'd', 'e'}; int want_order[] = {2, 4, 3, 0, 1}; int length = 5; OrderElements(chars, want_order, length); // chars now contains: c, e, d, a, b It's easy when you can use linear extra space, but can it be done with only constant extra space, i.e., ...

How does this MATLAB code work? (probabilities and random sequences)

I saw this code in a comment for the article "Never-ending Shuffled Sequence". I understand the basic premise, but I don't know how it works. The biggest explanation I need is of the first two lines of the while loop. (Because it is written in MATLAB I can only guess at how this code functions.) probabilities = [1 1 1 1 1 1]; unrandomn...

c++ vector random shuffle part of it

Whats the best way to shuffle a certain percentage of elements in a vector. Say I want 10% or 90% of the vector shuffled. Not necessarily the first 10% but just 10% across the board. TIA ...

Verify Knuth shuffle algorithm is as unbiased as possible

I'm implementing a Knuth shuffle for a C++ project I'm working on. I'm trying to get the most unbiased results from my shuffle (and I'm not an expert on (pseudo)random number generation). I just want to make sure this is the most unbiased shuffle implementation. draw_t is a byte type (typedef'd to unsigned char). items is the count of i...

oneliner scramble program

It's that time of year again that programmers want to shuffle a list such that no element resides on its original position (at least in the Netherlands, we celebrate Sinterklaas and pick straws for deciding who writes who a poem). Does anyone have a nice Python single statement for that? So, input example: range(10) Output example: [2,...

What is the best List implementation for Large lists in java

Hi, I have to create a large list of n elements (could be up to 100,000). each element in the list is an integer equivalent to the index of the list. After this I have to call Collections.shuffle on this list. My question is, which list implementation (either java collections or apache collections) should be used. My gut feeling is Ar...

Can I choose some(not all) elements in an array and shuffle it in PHP?

Can I choose some elements in an array and shuffle it in PHP? You know, when you use shuffle(array) , It shuffles all elements in an array, but I just want to shuffle some elements in an array while keep other elements unchanged, how to do it? ...

Why does my PHP code not work?

Below is the code: function swap(&$a, &$b) { list($a, $b) = array($b, $a); } for ($i=0; count($resultset);$i++) { for($j=1;$j<5;$j++) { $k = rand(1, 4); swap($resultset[$i]["option".$j],$resultset[$i]["option".$k]); } } It is a two-dimensional array from a MySQL query, I want to shuffle the values ...

How to randomly sort (scramble) an array in Ruby?

I'd like to have my array items scrambled. Something like this: [1,2,3,4].scramble => [2,1,3,4] [1,2,3,4].scramble => [3,1,2,4] [1,2,3,4].scramble => [4,2,3,1] and so on, randomly ...

Is there an algorithm which prints out a shuffled list without actually modifing the list?

After reading this question I started to wonder: is it possible to have a shuffling algorithm which does not modify or copy the original list? To make it clear: Imagine you are given a list of objects. The list size can be arbitrary, but assume it's pretty large (say, 10,000,000 items). You need to print out the items of the list in ra...

shuffle Objects php

how can i sort object in php? i tryed shuffle but that expects an array Warning: shuffle() expects parameter 1 to be array, object given in /var/www/index.php on line 366 Warning: Invalid argument supplied for foreach() in /var/www/index.php on line 334 here is the code public function updateStatusWithoutDB() { $this->upda...

Way to quickly shuffle the numbers 1 through 4?

Looking to shuffle four variables (trying to change the order they appear in on a multiple choice list). I've been poking around for a while but I can't quite get my head around the logic, and looking up random shuffles in past questions gives super-detailed algorithms that are beyond my newbie skills (and the needs of this program I'm ...

How can I shuffle the lines of a text file in Unix command line?

I searched SO for a similar Q/A to no avail. I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines. How can I do that with cat, awk, cut, etc.? ...

Java's Collections.shuffle is doing what?

I recently found myself needing to be sure my list wasn't in order. Hibernate was nice enough to return it in perfect order. Silly hibernate, not reading my mind. I looked at my Java API and it tells me its shuffle method does this: Randomly permutes the specified list using a default source of randomness. Being the curious george t...

Need a repeatable random array shuffle using a key

I am looking to randomly shuffle a list/array using a key. I want to be able to repeat the same random order using the key. So I will randomly generate a numeric key from say 1 to 20 then use that key to try and randomly shuffle the list. I first tried just using the key to keep iterating through my list, decrementing the key until=0,...

Shuffle List<T>

Hi All I have a list which contains many thousands of FilePath's to locations of audio files, and was wondering which would be the most efficient way to "shuffle" a List? Any help is greatly appreciated :) Thank you ...

How can I randomise <li> elements on page refresh?

Does anyone have any ideas about how I could go about randomising an list each time the page is refreshed? A jQuery solution would be perfect as I'm already using it. Any advice appreciated! ...