Tags "shuffle" Result

How to randomize (shuffle) a JavaScript array?

I have one array like this: var arr1 = ["a", "b", "c", "d"]; How can I randomize / shuffle it? Solution The de-facto unbiased shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle. See https://github.com/coolaj86/knuth-shuffle You can see a great visualization here (and the original post linked to this) function shuffle(array) { var cu

it1352 0 2019-05-08

PHP shuffle with seed?

How can I make the php shuffle function use a seed, so that when I use the same seed, the shuffle function will output the same array. I read that shuffle is automatically seeded. Is there a way to get the seed of that shuffle used, or how can I create/mimic shuffle with a custom seed? Solution You can't retrieve the seed used by shuffle, but you

it1352 1 2020-07-03

How to generate non repeating random number

I am trying to randomize numbers in an array. I am able to do that using arc4random() % [indexes count] My problem is - If an array consists of 20 items, every time the array shuffles, in a batch of 5, different number should appear. Example : first shuffle: 1,4,2,5,6. second shuffle: 7,12,9,15,3 -(IBAction)randomNumbers:(UIButton *)sender {

it1352 3 2019-05-13

Shuffling an array in objective-c

Possible Duplicate: What’s the Best Way to Shuffle an NSMutableArray? I develop apps for iphone/iPad.I want to shuffle the objects stored in an NSArray.Is there any way to achieve it with objective-c? Solution Add a category to NSMutableArray, with code provided by Kristopher Johnson - // NSMutableArray_Shuffling.h #if TARGET_OS_IPH

it1352 0 2019-05-21

How to shuffle an array in PHP while still knowing the original index?

How do you shuffle a PHP array while still knowing the original index? I have a simple array in PHP (e.g. ["a", "b", "c", [5, 10]]). The shuffle() function allows you to shuffle the elements. However I still wish know what the original index was. How can I do this? Solution See shuffle doc 2nd example: <?php function shuffle_assoc(&am

it1352 3 2019-05-08

jQuery select random elements with same class

I have elements with class "selectElement". When I click on element with that class, I "select" it, and give it another class "selectedElements", if it doesn't already have it. But, I have a button that should randomly select certain number (e.g. 10) of elements with class "selectElement" and give them the "selectedElement" class. I tried somethi

it1352 0 2019-05-13

How do I shuffle an array in Swift?

How do I randomize or shuffle the elements within an array in Swift? For example, if my array consists of 52 playing cards, I want to shuffle the array in order to shuffle the deck. Solution This answer details how to shuffle with a fast and uniform algorithm (Fisher-Yates) in Swift 4.2 and how to add the same feature in the various previous vers

it1352 0 2020-07-17

Can I make a Deterministic Shuffle in clojure?

I'd like to make some shuffles of sets which will be the same every time my program is run: This is one way to do it: (def colours ["red" "blue" "green" "yellow" "cyan" "magenta" "black" "white"]) (defn colour-shuffle [n] (let [cs (nth (clojure.math.combinatorics/permutations colours) n)] [(first cs) (drop 1 cs)])) ; use (rand-int 40320

it1352 3 2019-05-13

Randomizing arg order for a bash for statement

I have a bash script that processes all of the files in a directory using a loop like for i in *.txt do ops..... done There are thousands of files and they are always processed in alphanumerical order because of '*.txt' expansion. Is there a simple way to random the order and still insure that I process all of the files only once? Solution

it1352 0 2019-05-09

Shuffle and re shuffle list in python?

I have a list of numbers and I want to shuffle it with a key and redo it. I am using it as a small encryption algorithm so I need to re-shuffle or get the original list from the suffled list. original = [10, 20, 30, 25, 45, 68, 25] shuffled = shuffle(original, key=10) print shuffled # >>> [25, 30, 25, 10, 20, 45, 68] print re_shuffle(shuf

it1352 0 2019-05-07