Generation of random numbers in dwm1001

I am currently trying to generate random IDs for dwm1001 devices. I’ve tried the typical way of number generation using srand(time(NULL)), however, I keep getting the following:

I tried to find and try out other solutions, but none of them worked. Is there a way to generate numbers, or to fix this problem?

Looks like you issue isn’t with the rand function itself, it’s with seeding the random number generator so you don’t get the same numbers every time.

Any other semi-random source you could use? If there is any external interaction (user key press, signing on to a network etc…) then they would introduce enough variability that using the time since startup in microseconds or any smaller units would work. You could even read the current time from the DW1000, that will give you a high rate clock that it doesn’t take much of a difference in startup time to get a different value from.
If you have an analog input then set it to roughly mid rail using some large resistors so the result is noisy. Read it 32 times, use the least significant bits from each reading to generate a number to seed rand() with.

Hello Andy,

I tried using the clock ticks as the seed and performing some operations on it, but I kept on getting the same results. Giving an input is not really ideal for my case, since i want to connect the devices to external power sources. One thing I am currently trying to do is generate an ID on the initiator’s side, and sending it back to the responders.

Without some external source to give you randomness it can be tricky to get a random number. It’s almost as if they want computers to be repeatable and deterministic.

Maybe look for variabilities in crystal frequencies. Read the DW internal time, wait a fixed time on your processor clock (longer is better if possible) and then read the DW time again. Take the difference in the two DW times. (Or maybe just read the DW time once, you may get some variability due to PLL lock times being different).

While the difference won’t be huge you should get some small variation each time you do this process due to the two clocks running at very slightly different speeds and that speed difference being related to things like the temperatures of the two crystals. The same unit won’t see much variation each time but you should get a bigger variation between two units.
You’d need to test to see how much variability this gives but it should be good for a few bits of randomness in the least significant bits. Then repeat a couple of times to give more bits.

1 Like

Thats quite an interesting way to approach this problem. I’ll be sure to give it a try if my other solution is not too practical.

Thank you for this information and for your help!