You indexing in that for() loop isn't right.
You need to add 2 to i on every pass or
increment it in the second [i+1] access,
and assuming "size" is the length of "array"
you then shouldn't divide by 2 either...

Or you can just throw away the top 16 bits of
each rand16 to make it easier:

    uint8_t array[size];
    for (i=0; i < size; i++)
    {
          aux = call Random.rand16();
          array[i] = aux & 0xff;
    }

I would do some study of the actual values
produced by rand16() to see how random each
byte is as well. I've seen (very) simple
pseudo-random generators that have large
jumps between values that are not as random
as one might like.

MS



Ruben Rios wrote:
> Hello everyone,
> 
> I have seen the Random interface generates random numbers of 16 or 32
> bits long. How can I generate a 8 bit random numbers array?
> 
> void generateRandomArray(uint8_t * array, uint8_t size)
> {
>    uint8_t i;
>    uint16_t aux;
>    uint8_t *p = (uint8_t *)&aux;
> 
>     for (i=0; i < size / 2; i++)
>    {
>          aux = call Random.rand16();
>          array[i] = *p;          //Store the MSB
>          array[i+1] = *(p+1); //Store the LSB
>    }
> }
> 
> I am not very good at pointers that's why I don't know if this works
> fine. By the way, I guess the array contents are modified within the
> function but these are also valid outside, right?
> 
> Thanks in advance
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to