Ask coding questions

← Back to all posts
C++ srand() Random
LukeBreeden (0)

Hi, i'm working on a dice rolling project where I need random numbers for the rolls, and different numbers each time it's run. I know I need to change the srand(), but i don't know how to make the seed random. How do I make the srand() actually random?

Answered by AdobeZev (8) [earned 5 cycles]
View Answer
Commentshotnewtop
AdobeZev (8)

Use the time module
#include <stdlib.h>
#include <time.h>
srand(time(NULL))

LukeBreeden (0)

@AdobeZev Thanks, this helped a lot. I'm new to C++ and am just getting into other libraries. The only thing that I could think of was using rand(), but then it isn't random, so it would be an infinite loop of srand(rand()). This helps A LOT!