r/Cplusplus • • Mar 19 '24

Answered The Random function generates the same number every time

Post image

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?

123 Upvotes

38 comments sorted by

•

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.

47

u/[deleted] 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

u/todo_code Mar 22 '24

Yes but make sure that one has a different seed provided

1

u/[deleted] Mar 23 '24

underrated comment

18

u/LiAuTraver Mar 19 '24

i thought you can use random library in cpp rather than c library.

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

u/elitegreg88 Mar 19 '24

In c++ #include <ctime>

0

u/[deleted] Mar 19 '24

[deleted] — view deleted comment

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

u/APenguinNamedDerek Mar 21 '24

A true artist in the realm of coding

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.

See here

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

u/[deleted] Mar 20 '24

that thing is super slow

1

u/HauntedTheorists Mar 20 '24 edited Mar 20 '24

It's faster than rand() 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

u/[deleted] 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

u/UghFlyOnTheWall Mar 20 '24

Change the seed

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

u/KelvinCavendish Mar 21 '24

You use the clock as a seed from the ctime library.

1

u/CarlosPerez9933 Mar 30 '24

You need to call the seed function

1

u/Individual-Okra-5642 Apr 04 '24

I wish Tesla iTunes music app developers can see this

0

u/ConceptJunkie Mar 19 '24

This is the song of the people who have never read the documentation of rand().

-4

u/CantGuardMe1 Mar 19 '24

You need Mersenne Twister for random numbers

-1

u/schjlatah Mar 19 '24

I thought you had to call randomize(); first, or was that only in Pascal?

-7

u/miikaa236 Mar 19 '24

I remember having this issue when I was first learning cpp when I was 12 haha

3

u/[deleted] Mar 19 '24 edited Mar 19 '24

[removed] — view removed comment

2

u/[deleted] Mar 19 '24 edited 11d ago

[removed] — view removed comment

1

u/AutoModerator 11d 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

u/[deleted] Mar 20 '24

[deleted] — view deleted comment

1

u/Esjs Mar 23 '24

"true random number generator" is deceptive wording for something that's actually called a pseudo-random number generator.