Home/Support/Support Forum/How to generate a random number on 9215.
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

How to generate a random number on 9215.

0 votes
I am trying to generate a random number in the 0-255 range. I am using srand and rand. In order to generate the seed with srand I am using a value taken from the ticks passed since board was on.

If I run my program using JTag it will work correctly. This means that every time I run it, I will get a different number.

In the other hand, when I download the code to the board and run it without JTAG, I am getting the same result on every run. I check the ticks passed and I am using different numbers to create the seed on every run.

Any solution to generate a random number each time?

Thanks,
asked May 1, 2014 in Embedded Devices by hsantana New to the Community (22 points)

Please log in or register to answer this question.

1 Answer

0 votes
Hello

There are a couple of things that come to mind, they could also be used in combination.

1) when calling srand pass it as a parameter the current system time.

2) There is an undocumented call entitled GetFullEth0Stats. it is located in \bsp\devices\common\ethernet\naeth.c. YOU pass it a pointer to an ETH_STATS structure (see \h\efe_def.h). An ETH_STATS structure contains all sorts of juicy ethernet statistics. You could call this and add a couple of the statistics together and use that as a param in calling srand.

3) Pass the system time added to a sum of a couple of the ethernet stats as described above to srand.

see if that/those help.
answered May 2, 2014 by dakotas_dad Veteran of the Digi Community (694 points)
...