2024-05-01 11:26 AM
Hi,
I want to use random number.
But, the same random number is generated each time it is run.
When I debug the code below, W change with the same pattern every time I debug.
What's the problem and is there any solution to make random number which is not depend on the order?
/* USER CODE BEGIN 0 */
double uniform_rand(){
return (double)rand()/((double)RAND_MAX + 1);
}
/* USER CODE END 0 */
int main(void) {
/* USER CODE BEGIN 2 */
srand(time(NULL));
/* USER CODE END 2 */
while {
W = uniform_rand();
}
}
Solved! Go to Solution.
2024-05-02 03:25 AM
I'm using 2 kinds of board for i2c.
One is U575ZI-Q for master and the other is L432KC for slave.
Hello,
The two devices feature RNG (random number generator) as shown in their respective DS:
You can inspire from one of the examples of RNG. Example:
2024-05-01 11:30 AM
Hello,
What product you're using so you can use RNG peripheral instead of that "rand" library?
2024-05-01 11:34 AM
sorry but I can't understand what you're saying...
isn't it "rand" library?
srand(time(NULL));
2024-05-01 11:50 AM
rand() from stdlib.h
What MCU part number are you using?
2024-05-01 01:47 PM
Need to seed it better.
Is there a clock or anything behind time() ? There'd need to be some association with the RTC and it would need to be running.
2024-05-01 06:48 PM
Yes I use rand() function from stdlib.h
I'm using 2 kinds of board for i2c.
One is U575ZI-Q for master and the other is L432KC for slave.
2024-05-01 06:56 PM
I didn't use anything according to the clock.
Is there any recommandation to make better seed?
srand(time(NULL)); <----------- I think it change seed every single second
2024-05-01 07:14 PM
That supposes time() is working and there's anything making it tick.
In embedded you'd actually need to facilitate this.
You should confirm that it is working as you imagine.
2024-05-02 03:25 AM
I'm using 2 kinds of board for i2c.
One is U575ZI-Q for master and the other is L432KC for slave.
Hello,
The two devices feature RNG (random number generator) as shown in their respective DS:
You can inspire from one of the examples of RNG. Example: