开发者

Random Number Generation - C on ARM7

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, howe开发者_Python百科ver 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!


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:

static unsigned int next = 1;

int rand_r(unsigned int *seed)
{
       *seed = *seed * 1103515245 + 12345;
       return (*seed % ((unsigned int)RAND_MAX + 1));
}

int rand(void)
{
       return (rand_r(&next));
}

void srand(unsigned int seed)
{
       next = seed;
}


The standard library function is rand(), not Random(); 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜