draw
从数组中随机抽取一个元素。
语法
typescript
draw<T>(
array: T[]
): T | undefined
参数
array
(T[]): 要抽取的数组
返回值
T | undefined
: 随机抽取的元素,如果数组为空则返回 undefined
示例
基本用法
typescript
import { draw } from 'radash'
const fruits = ['apple', 'banana', 'cherry', 'date']
const randomFruit = draw(fruits)
// 可能返回: 'apple', 'banana', 'cherry', 或 'date'
处理空数组
typescript
import { draw } from 'radash'
const emptyArray: string[] = []
const result = draw(emptyArray)
// undefined
处理单个元素数组
typescript
import { draw } from 'radash'
const singleElement = ['hello']
const result = draw(singleElement)
// 'hello'
处理数字数组
typescript
import { draw } from 'radash'
const numbers = [1, 2, 3, 4, 5, 6]
const randomNumber = draw(numbers)
// 可能返回: 1, 2, 3, 4, 5, 或 6
处理对象数组
typescript
import { draw } from 'radash'
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
]
const randomUser = draw(users)
// 可能返回: { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, 或 { id: 3, name: 'Charlie' }
处理混合类型数组
typescript
import { draw } from 'radash'
const mixed = ['hello', 42, true, { key: 'value' }]
const randomItem = draw(mixed)
// 可能返回: 'hello', 42, true, 或 { key: 'value' }
游戏应用
typescript
import { draw } 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♣'
]
drawCard(): string | undefined {
return draw(this.deck)
}
}
const game = new CardGame()
const card = game.drawCard()
// 可能返回: 'A♠', '2♥', 'K♦', 等
随机选择器
typescript
import { draw } from 'radash'
class RandomSelector<T> {
private items: T[]
constructor(items: T[]) {
this.items = [...items]
}
select(): T | undefined {
return draw(this.items)
}
selectMultiple(count: number): T[] {
const result: T[] = []
for (let i = 0; i < count && this.items.length > 0; i++) {
const item = draw(this.items)
if (item !== undefined) {
result.push(item)
// 从数组中移除已选择的项目
const index = this.items.indexOf(item)
if (index > -1) {
this.items.splice(index, 1)
}
}
}
return result
}
}
const selector = new RandomSelector(['red', 'green', 'blue', 'yellow'])
const color = selector.select()
// 可能返回: 'red', 'green', 'blue', 或 'yellow'
随机任务分配
typescript
import { draw } 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 assignRandomTask(): Task | undefined {
return draw(tasks)
}
const assignedTask = assignRandomTask()
// 可能返回任意一个任务
随机颜色生成
typescript
import { draw } from 'radash'
const colors = [
'#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
'#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9'
]
function getRandomColor(): string | undefined {
return draw(colors)
}
const color = getRandomColor()
// 可能返回任意一个颜色值
随机问候语
typescript
import { draw } from 'radash'
const greetings = [
'Hello!',
'Hi there!',
'Good morning!',
'Good afternoon!',
'Good evening!',
'Hey!',
'Greetings!',
'Welcome!'
]
function getRandomGreeting(): string | undefined {
return draw(greetings)
}
const greeting = getRandomGreeting()
// 可能返回任意一个问候语
随机笑话
typescript
import { draw } 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 | undefined {
return draw(jokes)
}
const joke = getRandomJoke()
// 可能返回任意一个笑话
随机密码生成
typescript
import { draw } from 'radash'
const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
const numbers = '0123456789'
const symbols = '!@#$%^&*()_+-=[]{}|;:,.<>?'
function generatePassword(length: number): string {
const allChars = letters + numbers + symbols
let password = ''
for (let i = 0; i < length; i++) {
const char = draw(allChars.split(''))
if (char !== undefined) {
password += char
}
}
return password
}
const password = generatePassword(8)
// 生成8位随机密码
注意事项
- 随机性: 每次调用都可能返回不同的结果
- 空数组: 如果数组为空,返回 undefined
- 不修改原数组: 不会修改原始数组
- 类型安全: 支持完整的 TypeScript 类型推断
与其他函数的区别
draw
: 随机抽取一个元素shuffle
: 随机打乱数组顺序random
: 生成随机数字sample
: 随机抽取多个元素
性能
- 时间复杂度: O(1)
- 空间复杂度: O(1)
- 适用场景: 随机选择、游戏逻辑、随机化