You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言实现无连续重复且数量均等的颜色随机抽样

解决思路与实现代码

问题核心

你当前的随机抽样方法无法保证每种颜色严格出现92次,单纯循环重试效率极低,甚至可能因为剩余颜色的分布问题陷入死循环。我们需要换用构造式生成的思路,同时满足两个约束条件。

方案1:迭代调整法(简单易理解)

先构造数量准确的颜色池,再打乱后调整连续重复的位置:

步骤1:生成数量准确的颜色池

values <- c("blue", "red", "green", "yellow")
count_per_color <- 92
# 创建每种颜色重复92次的向量
color_pool <- rep(values, each = count_per_color)

步骤2:打乱并修正连续重复

set.seed(340) # 固定种子确保结果可复现
repeat {
  # 随机打乱颜色池
  shuffled <- sample(color_pool)
  # 找出所有连续重复的位置
  duplicate_positions <- which(shuffled[-1] == shuffled[-length(shuffled)])
  
  if (length(duplicate_positions) == 0) {
    break # 没有连续重复,跳出循环
  }
  
  # 逐个修正重复项:将重复项与后面第一个不同的颜色交换
  for (i in duplicate_positions) {
    # 找到后面第一个不等于当前颜色的位置
    swap_idx <- which(shuffled[(i+2):length(shuffled)] != shuffled[i])[1]
    if (!is.na(swap_idx)) {
      swap_idx <- swap_idx + i + 1
      # 执行交换
      temp <- shuffled[i+1]
      shuffled[i+1] <- shuffled[swap_idx]
      shuffled[swap_idx] <- temp
    }
  }
}

# 验证结果
table(shuffled) # 每种颜色恰好92次
any(shuffled[-1] == shuffled[-length(shuffled)]) # 返回FALSE,无连续重复

方案2:递归构造法(高效可靠)

通过递归逐步构建序列,每次只从允许的颜色(非上一个颜色且仍有剩余)中随机选择,天然满足两个约束:

build_valid_sequence <- function(remaining_counts, last_color = NULL) {
  # 所有颜色用完时返回空向量
  if (sum(remaining_counts) == 0) {
    return(character(0))
  }
  
  # 筛选可选颜色:排除上一个颜色,且剩余数量>0
  available_colors <- names(remaining_counts)[remaining_counts > 0 & names(remaining_counts) != last_color]
  
  # 随机选一个可选颜色
  chosen_color <- sample(available_colors, 1)
  
  # 减少该颜色的剩余数量
  remaining_counts[chosen_color] <- remaining_counts[chosen_color] - 1
  
  # 递归生成后续序列并拼接
  c(chosen_color, build_valid_sequence(remaining_counts, chosen_color))
}

# 初始化每种颜色的剩余数量
remaining <- setNames(rep(92, 4), values)
set.seed(340)
final_sequence <- build_valid_sequence(remaining)

# 验证结果
table(final_sequence)
any(final_sequence[-1] == final_sequence[-length(final_sequence)])

为什么原方法不行?

原代码每次只排除上一个颜色随机选择,没有控制整体的使用次数,最终的数量分布是随机的,很难刚好凑出每种92次。而上面的两种方法都先锁定了数量,再保证无连续重复,或者在选择时同时兼顾数量和连续约束。

内容的提问来源于stack exchange,提问作者Ann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 23:36:16