From: TTman on

"Go Rf" <Rf(a)Rf.org> wrote in message
news:4b029fd0$0$272$14726298(a)news.sunsite.dk...
> Hello all
>
>
>
> I need a function, when called returns a pseudo random number between X
> and Y.
>
>
>
> I have many boards that periodically broadcasts their board addresses on
> an RF link. All boards use the same RF-channel frequency, so data
> collisions is going to happen. So I need to randomly delay between those
> broadcasts to be able to avoid collisions with other boards broadcasts.
>
>
>
> I want to make a pseudo random delay (X ms - Y ms) before sending the
> package again.
>
use the devices address as a delay.... collisions will be random, and so
will the address.


From: Arlet on
On Tue, 17 Nov 2009 14:06:07 +0100, Go Rf wrote:

> Hello all
>
>
>
> I need a function, when called returns a pseudo random number between X and
> Y.
>

If you have a cheap (enough) 32 bit multiply, this method works pretty
well:

x(n)=a*x(n-1)+carry

Finding a value for 'a' that results in a large period is left as an
exercise.

> I have many boards that periodically broadcasts their board addresses on
> an RF link. All boards use the same RF-channel frequency, so data
> collisions is going to happen. So I need to randomly delay between those
> broadcasts to be able to avoid collisions with other boards broadcasts.

You'll probably also want exponential backoff after detecting a collision.
Everytime a board detects a collision (by lack of acknowledgement) it
should double the upper bound of its random generator.

An easy way to do this is to use an increasing number of bits from your
x(n) value above.



From: vladitx on
On Nov 17, 3:06 pm, "Go Rf" <R...(a)Rf.org> wrote:

> I need a function, when called returns a pseudo random number between X and
> Y.

For the interval [X, Y), assuming _unsigned_ integers and 'C' syntax:

random = X + (rnd() % (Y - X));

Where "rnd()" is your source of pseudo-random unsigned integer.
Choosing that is however dependent on your resources and expectations.
From: Hans-Bernhard Bröker on
vladitx wrote:
> On Nov 17, 3:06 pm, "Go Rf" <R...(a)Rf.org> wrote:
>
>> I need a function, when called returns a pseudo random number between X and
>> Y.
>
> For the interval [X, Y), assuming _unsigned_ integers and 'C' syntax:
>
> random = X + (rnd() % (Y - X));

An elegant formula, isn't it? Unfortunately it has a serious problem:
the result doesn't have the qualities usually requested from it. It's
not uniformly distributed unless (Y-X) just so happens to exactly divide
the range of the random number generator. Otherwise you get
(RND_MAX+1)%(Y-X) outcomes that are more probable than the others.
From: Ari Okkonen on
Recommended reading: Donald Knuth: Art of Computer Programming, part: Seminumerical Algorithms
BR Ari

Go Rf wrote:
> Hello all
>
>
>
> I need a function, when called returns a pseudo random number between X and
> Y.
....