Random Numbers

I'm doing an exercise (Accelerated C++), in which I want a function to generate random students. It does manage to create a random student, however, it does create only one. Say, my for() loop in main.cpp makes 5 iterations, then I will get the same student 5 times produced, instead of different ones.

I create my random seed in the function. I thought that perhaps the iteration was too fast which caused the random number generator (srand()) to produce the same seed with each iteration, hence I added a long running loop but that didn't work either.

My files look like this (only the important bits):

studentinfo.h
1
2
3
4
5
6
7
8
9
10
11
// ...

struct Student_info {
  std::string name;
  double midterm, finals;
  std::vector<double> homework;
};

// ...

struct Student_info randomStudentGenerator();


studentinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...

struct Student_info randomStudentGenerator() {
  srand(time(NULL));
  // list of many names
  std::string list_of_names[] = {"smith", "..." , "carpenter"};
	
  Student_info res;
  
  // initialize members with random values
  res.name = list_of_names[rand() % sizeof list_of_names/sizeof *list_of_names];
  res.midterm = rand() % 101;
  res.finals = rand() % 101;
  for(int i=0; i < 10; ++i)
    res.homework.push_back(rand() % 101);
	
  return res;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ...

std::vector<Student_info> students;
Student_info record;
std::string::size_type maxlen = 0; // length of the longest name in students

// ...

for(int i=0; i < 5; ++i) {
  // create a random student, check the length of its name and push it into students
  record = randomStudentGenerator();
  maxlen = std::max(maxlen, record.name.size());
  students.push_back(record);
  // for(int j=0; j < 1000000; ++j); ... I tried this to buy some time but no
}

// ... 
Last edited on
Remove the srand function from studentinfo.cpp and add it to the beginning of your main function. Computers are still incredibly fast, so adding a long-running loop still won't cut it. Also, doing such a thing makes your program unnecessarily inefficient.
srand(time(NULL)); your function is working way faster than time changes. So each new call will set random generator to exactly same state as previous and force function to produce same results.

you need to call srand() only once in your program: at the beginning of main().
(And think about using proper C++ random facilities instead of not really random biased naive C approach)
Much appreciated. It works now.

Yeah, I know that it becomes inefficient. But I was trying to find the source of error, so to speak.
@MiiNiPaa

What would that be? Right now I have the problem that, even though I put my srand() at the beginning of main(), I get some students produced multiple times. Most likely because my function works too fast.

It's all fine if I generate only 5 or 10 students, but with 1000 I get results like this:

studenta
studenta
...
studenta
studentb
studentb
...
studentb
...
etc.

I couldn't find anything else than srand() and rand(). Is there a way I can cause a different seed for every call to rand()?
Most likely because my function works too fast.
If you do not have srand in your function, then its speed is irrelevant. You have an error in your math.
rand() % sizeof list_of_names/sizeof *list_of_names is parsed as
1
2
(rand() % sizeof list_of_names)  /  
                                     sizeof *list_of_names
Ah, thank you!

Also, I did overlook something. The reason I thought I got the same student all over again was because my vector of students got sorted at some other point, which I haven't shown in the code snippets above.

Anyway, much appreciated for your help.
Topic archived. No new replies allowed.