Hi,
I am working with code warrior v10.6 and of MQX v4.0.
I am generating random numbers using srand(); followed by rand(); and it is generating random numbers perfectly.
But what problem I am facing is when module restart it again start to generate random number from scratch.(I am attaching snapshot)
Instead I want to escape those numbers which are already generated before module restart. Is there any Idea to do this??
I am doing this thing manually by storing last seed of srand function before my module reset but I dont want to do this...
Is there any ready made function available to do this??
Here is my code generating Random Numbers:
void Main_task(uint_32 initial_data)
{
srand(0);
while(1)
{
_time_delay(1000);
printf("rand number: %d \n",rand());
}
}
Hi,
Random number generator generates random instead of unique numbers.
It means that you can get two the same numbers in case of one random number sequence (with one seed).
You can use ranging by modulo.
random_x = rand() % 100 + 0;
after reset:
random_x = rand() % 100 + 100;
after reset:
random_x = rand() % 100 + 200;
…
However this doesn’t solve issue with two the same numbers between resets.
If you need unique number you will need compare generated number with all already generated numbers and discard when this number was already used.
Other option is that you will have field with unique numbers (e.g. all number in range 0~999) and this field will be shuffled by using random number generator. After that you can pick unique number one by one from this list.
Numbers will be unique and it will look like random numbers (in specific range).
Note: if you need really random numbers, you have to use really random seed. You can use RNGA module for generating random seed. Please check also RNGA module description in reference manual - chapter Overview. There is described how to use RNGA module for generation true random number (for true 32bit random number you will generate 32 random numbers but you will use just one bit from every number). Some of new Kinetis MCUs will have new version of this module which will generate true random numbers.
I hope it helps you.
Have a great day,
RadekS
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
A common trick to generate a different random sequence each time is generate a seed using time(): srand (time(NULL));
More info: http://www.cplusplus.com/reference/cstdlib/srand/
Better trick is to use an analog input.
You can read temperature, Vref, or any floating AD Pin.
Hi Jamie,
Thanks for reply.
In my code I tried to use sran(time(NULL)); but unfortunately it is not working !!!!.I dont know why??
Here is my code:
void Main_task(uint_32 initial_data)
{
while(1)
{
_time_delay(1000);
srand(time(NULL));
printf("Random Number: %d \n",rand());
}
}
Please note that I include time.h and stdlib.h in main.h file.
But one thing i got from your suggestion is getting a random number from time.
So what I did is.,
void Main_task(uint_32 initial_data)
{
TIME_STRUCT time_2;
DATE_STRUCT date;
//getting current time from PC
_time_set(&time_2);
while(1)
{
_time_delay(1000);
_time_get(&time_2);
srand(time_2.SECONDS);
printf("Random Number: %d \n",rand());
}
}
And I got Random Number as per time without repetition...!!!!!
Thnx Thnx...................
Regards,
Utsavi Bhauchwala.