rand()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.
- Labels:
-
STM32CubeIDE
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-02 3: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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 11:30 AM
Hello,
What product you're using so you can use RNG peripheral instead of that "rand" library?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 11:34 AM
sorry but I can't understand what you're saying...
isn't it "rand" library?
srand(time(NULL));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 11:50 AM
rand() from stdlib.h
What MCU part number are you using?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 1: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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 6: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 6: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-01 7: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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-05-02 3: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:
