shuffle
隨機打亂數組元素的順序。
語法
typescript
shuffle<T>(array: T[]): T[]
參數
array
(T[]): 要打亂的數組
返回值
T[]
: 打亂順序後的新數組
示例
基本用法
typescript
import { shuffle } from 'radash'
const numbers = [1, 2, 3, 4, 5]
const shuffled = shuffle(numbers)
// 可能返回: [3, 1, 5, 2, 4]
處理字符串數組
typescript
import { shuffle } from 'radash'
const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
const shuffledFruits = shuffle(fruits)
// 可能返回: ['cherry', 'apple', 'elderberry', 'banana', 'date']
處理對象數組
typescript
import { shuffle } from 'radash'
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
{ id: 4, name: 'Diana' }
]
const shuffledUsers = shuffle(users)
// 可能返回: [
// { id: 3, name: 'Charlie' },
// { id: 1, name: 'Alice' },
// { id: 4, name: 'Diana' },
// { id: 2, name: 'Bob' }
// ]
處理空數組
typescript
import { shuffle } from 'radash'
const emptyArray: number[] = []
const shuffled = shuffle(emptyArray)
// []
處理單個元素數組
typescript
import { shuffle } from 'radash'
const singleElement = ['hello']
const shuffled = shuffle(singleElement)
// ['hello'] (順序不變)
處理混合類型數組
typescript
import { shuffle } from 'radash'
const mixed = ['hello', 42, true, { key: 'value' }]
const shuffled = shuffle(mixed)
// 可能返回: [42, { key: 'value' }, 'hello', true]
游戲應用
typescript
import { shuffle } from 'radash'
class CardGame {
private deck = [
'A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠',
'A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥',
'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦',
'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣'
]
shuffleDeck() {
this.deck = shuffle(this.deck)
}
dealCards(count: number) {
return this.deck.slice(0, count)
}
}
const game = new CardGame()
game.shuffleDeck()
const hand = game.dealCards(5)
// 返回5張隨機牌
隨機選擇器
typescript
import { shuffle } from 'radash'
class RandomSelector<T> {
private items: T[]
constructor(items: T[]) {
this.items = [...items]
}
shuffle() {
this.items = shuffle(this.items)
}
select(count: number): T[] {
return this.items.slice(0, count)
}
selectAll(): T[] {
return shuffle(this.items)
}
}
const selector = new RandomSelector(['red', 'green', 'blue', 'yellow', 'purple'])
const colors = selector.select(3)
// 返回3個隨機顏色
隨機任務分配
typescript
import { shuffle } from 'radash'
interface Task {
id: number
name: string
priority: 'low' | 'medium' | 'high'
}
const tasks: Task[] = [
{ id: 1, name: 'Write documentation', priority: 'high' },
{ id: 2, name: 'Fix bug', priority: 'high' },
{ id: 3, name: 'Add tests', priority: 'medium' },
{ id: 4, name: 'Refactor code', priority: 'medium' },
{ id: 5, name: 'Update dependencies', priority: 'low' }
]
function assignRandomTasks(taskCount: number): Task[] {
const shuffledTasks = shuffle(tasks)
return shuffledTasks.slice(0, taskCount)
}
const assignedTasks = assignRandomTasks(3)
// 返回3個隨機任務
隨機顏色生成
typescript
import { shuffle } from 'radash'
const colors = [
'#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
'#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9'
]
function getRandomColors(count: number): string[] {
return shuffle(colors).slice(0, count)
}
const randomColors = getRandomColors(5)
// 返回5個隨機顏色
隨機問候語
typescript
import { shuffle } from 'radash'
const greetings = [
'Hello!',
'Hi there!',
'Good morning!',
'Good afternoon!',
'Good evening!',
'Hey!',
'Greetings!',
'Welcome!'
]
function getRandomGreeting(): string {
return shuffle(greetings)[0]
}
const greeting = getRandomGreeting()
// 返回一個隨機問候語
隨機笑話
typescript
import { shuffle } from 'radash'
const jokes = [
'Why don\'t scientists trust atoms? Because they make up everything!',
'Why did the scarecrow win an award? Because he was outstanding in his field!',
'Why don\'t eggs tell jokes? They\'d crack each other up!',
'What do you call a fake noodle? An impasta!',
'Why did the math book look so sad? Because it had too many problems!'
]
function getRandomJoke(): string {
return shuffle(jokes)[0]
}
const joke = getRandomJoke()
// 返回一個隨機笑話
隨機密碼生成
typescript
import { shuffle } from 'radash'
function generatePassword(length: number): string {
const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
const numbers = '0123456789'
const symbols = '!@#$%^&*()_+-=[]{}|;:,.<>?'
const allChars = letters + numbers + symbols
const shuffledChars = shuffle(allChars.split(''))
return shuffledChars.slice(0, length).join('')
}
const password = generatePassword(8)
// 生成8位隨機密碼
隨機測試數據
typescript
import { shuffle } from 'radash'
const testData = [
{ id: 1, value: 'test1' },
{ id: 2, value: 'test2' },
{ id: 3, value: 'test3' },
{ id: 4, value: 'test4' },
{ id: 5, value: 'test5' }
]
function getRandomTestData(count: number) {
return shuffle(testData).slice(0, count)
}
const randomData = getRandomTestData(3)
// 返回3個隨機測試數據
隨機排序
typescript
import { shuffle } from 'radash'
const students = [
{ name: 'Alice', grade: 85 },
{ name: 'Bob', grade: 92 },
{ name: 'Charlie', grade: 78 },
{ name: 'Diana', grade: 95 },
{ name: 'Eve', grade: 88 }
]
function randomizePresentationOrder() {
return shuffle(students)
}
const presentationOrder = randomizePresentationOrder()
// 返回隨機排序的學生列表
注意事項
- 不可變性: 不會修改原數組
- 隨機性: 每次調用都可能返回不同的結果
- 性能: 使用 Fisher-Yates 洗牌算法
- 類型安全: 支持完整的 TypeScript 類型推斷
與其他函數的區別
shuffle
: 隨機打亂數組順序draw
: 隨機抽取一個元素random
: 生成隨機數字sample
: 隨機抽取多個元素
性能
- 時間復雜度: O(n),其中 n 是數組長度
- 空間復雜度: O(n)
- 適用場景: 游戲、隨機化、測試數據生成