r/Cplusplus • u/ulti-shadow • Mar 19 '24
Answered The Random function generates the same number every time
So for a code I need a random number But every time I run the code, The numbers generated are the exact same. How do I fix it so that the numbers are different every time the code boots up?
105
u/jedwardsol Mar 19 '24
https://www.learncpp.com/cpp-tutorial/introduction-to-random-number-generation/
Scroll down to "seeding"
47
Mar 19 '24
you need to choose a seed for the random number generator that is different every time, or use another source of entropy
1
u/unlikely-contender Mar 21 '24
Where do I get the seeds from? Can I use a random number generator?
3
u/Charmander787 Mar 21 '24
time is typically used when you don’t need something cryptographically secure.
2
1
18
30
u/Nunuv_Yerbiz Mar 19 '24
You need to use a different seed. Put this as the first line in main():
srand(unsigned(time(NULL)));
It generates a seed based on the current time.
24
u/Mayedl10 Mar 19 '24
OP don't forget to
#include <time.h>also,
srand(time(NULL))should be enough.18
0
Mar 19 '24
[deleted]
8
u/HumanContinuity Mar 19 '24
Casting into the correct integer type for the seed I'd expect, but I just hit the keyboard until compilation errors go away so idk.
5
3
u/Nunuv_Yerbiz Mar 20 '24
time() returns the data type time_t, which is basically an undefined type. So, rather than your compiler bitching about it, it's just cast as unsigned to avoid compiler warnings.
6
u/quad99 Mar 19 '24
It’s supposed to work that way. It’s so you can use it for programs that need to be repeatable. Like simulations and statistics.
1
u/Tinbody Mar 21 '24
I’m curious why you would use a random number generator at all in those cases?
2
u/quad99 Mar 21 '24
Monte Carlo simulation. you might try variations of the model and need to repeat a test with the exact same data to compare the results. Or you need to add some random noise to a control system test and it needs to be repeatable after changes to the system model.
2
u/TOGoS Mar 21 '24
You wouldn't. You'd use a pseudo-random number generator, which is what `rand()` is. Pseudo-random number generators can pretend to be random number generators if you give them a different seed every time.
5
u/HauntedTheorists Mar 19 '24
You need srand first. But I recommend using std::mt19937 instead
0
Mar 20 '24
that thing is super slow
1
u/HauntedTheorists Mar 20 '24 edited Mar 20 '24
It's
fasterthanrand()and produces better results.
1
u/accuracy_frosty Mar 19 '24
It’s intentional, you’re meant to seed it so you can get the same result with the same number, also, using ints to store a value that can be represented with a 16 bit value lol
1
Mar 20 '24
that’s why it’s called pseudo random number generator! By the way, as others pointed out, just use seed every time before generating numbers.
1
1
u/brink1123 Mar 20 '24
Xoax.net c++ free courses they are a little dated but so informative. He has a video on the rand().
1
u/Ampnix Mar 20 '24
Brother Just put the seed incrementing by 1 as both the id of the seed and the int for increment. Therefore you will have a different number
for(int i = 0; i > -1; i++){
ranNum = (rand() % i) + 0;
std::cout << ranNum << std::endl;}
1
1
1
0
u/ConceptJunkie Mar 19 '24
This is the song of the people who have never read the documentation of rand().
-4
3
Mar 19 '24 edited Mar 19 '24
[removed] — view removed comment
2
Mar 19 '24 edited 16d ago
[removed] — view removed comment
1
u/AutoModerator 16d ago
Your comment has been removed because of this subreddit’s account requirements. You have not broken any rules, and your account is still active and in good standing. Please check your notifications for more information!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
Mar 20 '24
[deleted]
1
u/Esjs Mar 23 '24
"true random number generator" is deceptive wording for something that's actually called a pseudo-random number generator.

•
u/AutoModerator Mar 19 '24
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.
Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.
Homework help posts must be flaired with Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.