Uniform distribution versus naive shuffling?
php editor Xiaoxin will reveal the relationship between "uniform distribution and naive shuffling". In computer science, shuffling is an important operation often used to randomize data or collections. Uniform distribution means that the random number distribution is average within a certain range. So, can shuffling ensure even distribution? The answer is not simple, so let’s explore this question together.
Question content
I am shuffling a 3 int array 6 million times. I record each permutation of the array in a map. Below is the code using go.
package main import ( "fmt" "math/rand" "time" ) func randrange(min, max int) int { return rand.intn(max-min+1) + min } func naiveshuffle(arr *[3]int) { for i := 0; i < 3; i++ { e := randrange(0, 2) arr[e], arr[i] = arr[i], arr[e] } } func main() { rand.seed(time.now().unixnano()) m := make(map[[3]int]int, 6) arr := [3]int{-6,10,184} for i := 1; i <= 6000000; i++ { a := arr naiveshuffle(&arr) m[a]++ } for k, v := range m { fmt.println(k, ":", v) } }
Since I'm doing a simple shuffle, my understanding is that it shouldn't not produce an evenly distributed permutation. But this is what I get:
[184 -6 10] : 1000074 [184 10 -6] : 1000764 [-6 10 184] : 1000766 [10 184 -6] : 998090 [-6 184 10] : 1000479 [10 -6 184] : 999827
This shows that each of the 6 possible permutations occurs approximately 1 million times. Why does the distribution I get look uniform?
EDIT: Changed code to only seed once. I now get:
[-6 184 10] : 999507 [184 -6 10] : 1000401 [10 -6 184] : 1002163 [10 184 -6] : 999236 [-6 10 184] : 999016 [184 10 -6] : 999677
Edit 2: Thanks to Hobbs, I realized I made a stupid mistake. I should have shuffled a
, not arr
. I now get:
[10 -6 184] : 1111056 [-6 10 184] : 888442 [184 -6 10] : 888576 [10 184 -6] : 1109896 [-6 184 10] : 1113148 [184 10 -6] : 888882
Workaround
You shuffled arr
over 6 million times without restoring it to its original state between shuffles - In other words, your 6 million trials are not independent. Although the permutations of each shuffle are unevenly distributed, stacking these permutations on top of each other 6 million times produces a distribution that is very close to uniform.
The above is the detailed content of Uniform distribution versus naive shuffling?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are many ways to center Bootstrap pictures, and you don’t have to use Flexbox. If you only need to center horizontally, the text-center class is enough; if you need to center vertically or multiple elements, Flexbox or Grid is more suitable. Flexbox is less compatible and may increase complexity, while Grid is more powerful and has a higher learning cost. When choosing a method, you should weigh the pros and cons and choose the most suitable method according to your needs and preferences.

Top Ten Virtual Currency Trading Platforms 2025: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

The top ten cryptocurrency trading platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

The calculation of C35 is essentially combinatorial mathematics, representing the number of combinations selected from 3 of 5 elements. The calculation formula is C53 = 5! / (3! * 2!), which can be directly calculated by loops to improve efficiency and avoid overflow. In addition, understanding the nature of combinations and mastering efficient calculation methods is crucial to solving many problems in the fields of probability statistics, cryptography, algorithm design, etc.

Recommended safe virtual currency software apps: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

A safe and reliable digital currency platform: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.
