返回一个在指定范围内随机整数的 Go 函数,但要排除切片中的数字。

huangapple go评论87阅读模式
英文:

Go Function that returns a random int in a range, excluding numbers in a slice

问题

我正在尝试编写一个函数,该函数接受3个参数:最小数、最大数和在该范围内的黑名单数字片段。我已经能够通过生成一个新数字并将其与片段进行比较来“bruteforce”它,如果生成的数字与条目匹配,则再次比较。但这显然是一种非常低效的方法。当列表变得很大时,它会变得非常慢,因为它不断生成黑名单数字。有人有任何想法如何解决这个问题吗?

英文:

I am trying to make a function that takes 3 arguments. A minimum number, a maximum number and a slice of blacklisted numbers in between that range,I've been able to "bruteforce" it by generating a new number and comparing it to the slice again if the generated number matches an entry, but this is obviously a very inefficient way to do it.It gets very slow once the list grows large as it keeps generating blacklisted numbers. Does anyone have any idea how I could do it?

答案1

得分: 2

像这样应该可以满足你的需求:

import "math/rand"

func getRandomIntWithBlacklist(min int, max int, blacklisted []int) int {
  
  // 如果黑名单很大,你可能需要考虑缓存它
  excluded :=  map[int]bool{};
  for _,x := range blacklisted {
    excluded[x] = true
  }
  
  // 循环直到生成一个不在黑名单中的数字
  for {
    n := min + rand.Intn(max+1) // 生成满足 min <= n <= max 的数字
    if !excluded[n] {
      return n
    }
  } 
  
}

但是不要忘记给 rand 添加一些熵(除非你想要高度可重复的随机序列)。

英文:

Something like this ought to do you:

import &quot;math/rand&quot;

func getRandomIntWithBlacklist(min int, max int, blacklisted []int) int {
  
  // if blacklisted is/can be large, you might want to think about caching it
  excluded :=  map[int]bool{};
  for _,x := range blacklisted {
    excluded[x] = true
  }
  
  // loop until an n is generated that is not in the blacklist
  for {
    n := min + rand.Intn(max+1) // yields n such that min &lt;= n &lt;= max
    if !excluded[n] {
      return n
    }
  } 
  
}

But don't forget to see rand with some entropy (unless you want eminently repeatable random sequences.

答案2

得分: 1

根据您的查询,我使用了min、max和blacklisted参数构建了一个函数。代码创建了一个带有这些参数的函数,并返回一个整数数组。这些整数包括那些没有被禁止的数字,假设这是您所需要的。我还编写了一个includes函数,它返回一个布尔值,并接受两个输入,一个整数切片和一个整数值,因为这段代码使用整数。exclude函数从min循环到max,并调用contains函数;如果结果为false,则将其添加到列表中;如果结果为true,则在命令行中显示"Number is blacklisted, skipping"。然后返回整数切片。

func Exclude(min, max int, blacklisted []int) []int {
	var listOfNums []int
	log.Printf("min: %v, max: %v, blacklisted: %v\n", min, max, blacklisted)
	for i := min; i <= max; i++ {
		if !contains(blacklisted, i) {
			listOfNums = append(listOfNums, i)
		}else{
			log.Println("Number is blacklisted, skipping", i)
		}
	}
	return listOfNums
}
func contains(list []int, item int) bool {
	for _,v := range list {
		if v == item {
			return true
		}
	}
	return false
}
英文:

In response to your inquiry, I built a function using the min, max, and blacklisted arguments. As a result, the code creates a function with those parameters and returns an array of ints. The ints include the numbers that are not banned, presuming that is what you need. I also wrote an includes function that returns a bool value and takes two inputs, an int slice and an integer value, because this code utilizes integers. The exclude function cycles from min to max and calls the contains function; if it is not true, it adds it to the list; if it is true, it displays "Number is blacklisted, skipping" to the command line. The slices of integers are then returned.

func Exclude(min, max int, blacklisted []int) []int {
	var listOfNums []int
	log.Printf(&quot;min: %v, max: %v, blacklisted: %v\n&quot;, min, max, blacklisted)
	for i := min; i &lt;= max; i++ {
		if !contains(blacklisted, i) {
			listOfNums = append(listOfNums, i)
		}else{
			log.Println(&quot;Number is blacklisted, skipping&quot;, i)
		}
	}
	return listOfNums
}
func contains(list []int, item int) bool {
	for _,v := range list {
		if v == item {
			return true
		}
	}
	return false
}

huangapple
  • 本文由 发表于 2022年3月17日 07:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/71505223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定