0

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!

Draineh
  • 599
  • 3
  • 11
  • 28
  • 2
    You could scale the large number down by: `(bignumber % 111) + 10` – Gavin H Mar 25 '11 at 19:44
  • Not exactly the same thing, but read this http://stackoverflow.com/questions/137783/expand-a-random-range-from-1-5-to-1-7/891304#891304 – pmg Mar 25 '11 at 19:51
  • You can start with `& 0x7F` to get the number of bits you care about, then use @Gavin's approach. Depending on the range you really need, you might not even have to do the mod. – nmichaels Mar 25 '11 at 19:52
  • @nmichaels Sorry, I'm not familiar with `& 0x7F` ? I just tried a google for it but with little success! – Draineh Mar 25 '11 at 19:55
  • @pmg Thanks, whilst not exactly the same its still an interesting read, I'll favourite that away for a longer night :) – Draineh Mar 25 '11 at 19:56
  • @Draineh: I meant taking the bitwise AND of 0x7F and your number. I guess that wasn't obvious. – nmichaels Mar 25 '11 at 19:59
  • Using the `%` operator is not such a good idea since as this focuses on the lower bits, see http://members.cox.net/deleyd/random/crandom.html for a more about this. – hlovdal Mar 25 '11 at 20:00

2 Answers2

2

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;
}
Blindy
  • 65,249
  • 10
  • 91
  • 131
0

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165