hi

My random number function is meant to loop 6 times & output 6 different numbers. But instead it outputs 6 of the same numbers. How do I fix it? :)

Any advice would be really helpful.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>


using namespace std;

int random(int max)
{
	srand(time(NULL));

	return ((rand()%max)+1);
}

int main() 
{

for (int i=0; i<6; i++) {
		used_nos[i] = random(40);
		cout << used_nos[i] << endl;
	}

return 0;
}

Dani AI

Generated

: is correct — reseeding inside the helper causes the repeated values. A few additional, practical points to make the program robust and to cover the “six different numbers” requirement.

First, make sure used_nos actually exists and has room for six integers (for example int used_nos[6]; or std::vector<int> used_nos(6);). Out-of-bounds writes or uninitialized storage will hide themselves as weird behavior. Also avoid naming your function random (it can clash with platform/library symbols); pick a clearer name like randInRange or use the standard facilities below.

For better-quality and portable randomness, prefer the C++11 <random> library rather than rand(). Example that produces 6 values in 1..40 (may include repeats):

#include <random>
#include <vector>
#include <iostream>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(1, 40);

std::vector<int> used_nos(6);
for (int i = 0; i < 6; ++i) {
    used_nos[i] = dist(gen);
    std::cout << used_nos[i] << '\n';
}

If you truly need six distinct numbers, the simplest, efficient approach is to shuffle the range 1..40 and take the first six:

#include <algorithm>
#include <numeric>
#include <random>
#include <vector>
#include <iostream>

std::vector<int> pool(40);
std::iota(pool.begin(), pool.end(), 1); // 1..40
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(pool.begin(), pool.end(), gen);
for (int i = 0; i < 6; ++i) std::cout << pool[i] << '\n';

Final checks: verify max is positive, avoid reseeding inside a loop, and compile with warnings enabled — they catch many accidental bugs.

I'd imagine if you were to wait in the for loop for about a second per iteration it'd be different.

The problem is that you're reseeding the rand function so quickly it's being reseeded off the same value. You only need to seed it once, so do it at the start of main.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>


using namespace std;

int random(int max)
{

	return ((rand()%max)+1);
}

int main() 
{

	srand(time(NULL));
for (int i=0; i<6; i++) {
		used_nos[i] = random(40);
		cout << used_nos[i] << endl;
	}

return 0;
}

For further clarification... srand takes time(null) in your code, which updates every second, I believe. So if srand was in the function and if the loop executed for over a second you'd get different numbers out every different second.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.