As title, I notice that the output of the first call to rand()
seems to be a 9-digit number that steadily increases across the runs. The second call seems normal. Is this something well-known?
This is the code on my Mac
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
printf("%dn", rand());
printf("%dn", rand());
return 0;
}
Here is my Makefile
BINARIES := test
all: $(BINARIES)
clean:
$(RM) $(BINARIES) *~ *.dSYM
CXXFLAGS := -Wall -g -std=gnu++11 -O3
CXX := g++
RM := rm -r
This is the output of 8 runs
➜ Coding ./test
830930336
360000711
➜ Coding ./test
830963950
924951209
➜ Coding ./test
830980757
1207426458
➜ Coding ./test
831014371
1772376956
➜ Coding ./test
831064792
472319056
➜ Coding ./test
831081599
754794305
➜ Coding ./test
831098406
1037269554
➜ Coding ./test
831115213
1319744803
I tried srand(time(0))
and it does not change anything.
New contributor
Shen-Fu Tsai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8