随机数生成 - ARM7 上的 C
我需要生成一个随机数,现在这个数字需要在 10 到 120 秒之间。 现在,在 CI 中可以使用 Random 来完成此操作,但是我无权访问该函数。 为了尝试变得聪明,我识别了一些随机数据,我可以使用无线扫描功能(实际上最终需要这个随机数),它为我提供了每个检测到的 Wi-Fi 信号的信号强度。
使用这个我想我可以创建一个很好的随机数,但是显然这给出了一个非常大的总和,需要稍微缩小 - 这减少了不同随机数之间的潜在差异。
该随机数将用作尝试相互互连的不同无线设备的退避计时器,并且显然是我可以实现的更好的随机数字。
有什么想法吗?也许有更简单的方法来实现这一目标? 感谢您的任何提示。
编辑:为了使帖子可读!
I need to generate a random number, now this number needs to be somewhere between 10 and 120 seconds.
Now, in C I could use Random to acomplish this, however I do not have access to that function.
In an effort to try and be clever I have identified some random data, I have access to a wireless scan function (that this random number is actually eventually required for) which provides me the signal strength of each detected Wi-Fi signal.
Using this I thought I could create a nice random number, however obviously this gives a very large sum which needs to be scaled down somewhat - this reduces the potential difference between different random numbers.
The random number will be used as a backoff timer for different wireless devices trying to interconnect with eachother and obviously as random a figure as I can achieve the better.
Any thoughts? Maybe there is an easier method of achieving this?
Thanks for any tips.
Edit: To make the post readable!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对所有信号强度应用异或并将其转换为整数或您需要的任何值。
或者,这或多或少是 C 中定义
rand()
的方式:Apply a XOR on all the signal strengths and cast it to an integer or whatever you need.
Alternatively, this is more or less how
rand()
is defined in C:标准库函数是
rand()
,而不是Random()
;你为什么不能用它?尽管存在所有潜在缺陷,但对于后退计时器而言,它可能已经足够了,因为启动时间本身是通过其他设备的冲突和异步操作来随机化的。人们可能会对这个函数的上述缺陷赞不绝口,更会在其返回值中使用朴素的模运算,但它是关于目的的适用性和充分性的,而 rand() 在这种情况下绰绰有余。
The standard library function is
rand()
, notRandom()
; why can't you use that? For all its potential flaws it is probably more than adequate for a back-off timer, since the start time itself is randomised by the collision, and asynchronous action of the other devices.People will probably wax lyrical about the said flaws of this function, and more again on using a naive modulo operation in its return value, but it is about fitness for purpose and adequacy, and rand() is more than adquate in this case.