生成唯一的随机数从除特定的一个用C [英] Generating unique random numbers except from a specific one in C

查看:100
本文介绍了生成唯一的随机数从除特定的一个用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,我怎么能产生除了从特定的一个独特的随机数。例如,如果我想生成号码范围 1到10,除了从3 ,输出应该是这样的:

  7 6 1 2 4 9 5 8 10


解决方案

随机数字1 - 10,取出3

如果你之前或之后洗牌取出3不要紧。

另外,洗牌数字1 - 9和3重新标记为10 ...

有关不带偏见,您可以使用如费雪耶茨洗牌算法。 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

I was wondering, how can I generate unique random numbers except from a specific one. For example, if I want to generate numbers in range 1 to 10 except from 3, the output should be something like this:

7 6 1 2 4 9 5 8 10

解决方案

Shuffle the numbers 1 - 10 and remove 3.

It doesn't matter if you remove the 3 before or after shuffling.

Alternatively, shuffle the numbers 1 - 9 and relabel 3 as 10...

For shuffling without bias you can use for example the Fisher-Yates algorithm. http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

这篇关于生成唯一的随机数从除特定的一个用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆