hey guys,
I want to create 6 different random numbers without repetitions and this what I have done. I just wanted to make sure if this logic makes sense.

for(int n=0 ; n<6 ; n++)
	{
		randomInt[n] = generateRandomInt(0,20);
		cout << randomInt[n] << endl;
	}
	for(int i=0 ; i<6; i++)
	{
		for(int j=i+1 ; j<6 ; j++)
		{
			if(randomInt[i] == randomInt[j])
				randomInt[i]=generateRandomInt(0,20);
		}
		cout << randomInt[i] << endl;
	}
}

I see where you're trying to go with it, so I guess it does make sense. Why do you ask?

I ask because it works alright for larger range, but If I change the range to 0 to 4 instead of 0 to 20. I do get repetition. Is it possible to modify it a bit to get 100% no repetition?

Your easiest bet is to do a random shuffle of the range:

#include <algorithm>
#include <iostream>

int main()
{
  const int n = 4;
  int a[n];

  for ( int i = 0; i < n; i++ )
    a[i] = i;

  std::random_shuffle ( a, a + n );

  for ( int i = 0; i < n; i++ )
    std::cout<< a[i] <<'\n';
}

would that work for larger range, if change n to 100? My goal is to create 5 random numbers from 1 to 100 without repetition.

It'll work for any range, within reason. The limiting factor is storage for N integers, so when you want a range of [0,100000000), for example, the random shuffle solution becomes less useful.

so, how would I go about modify random shuffle code that you wrote, for 5 random numbers and range [0,100]?

>range [0,100]?
Just making sure, you want the range of 0 to 100, including both 0 and 100? There's a difference between [0,100] and [0,100), and that changes the code. For the latter it would be this:

#include <algorithm>
#include <iostream>

int main()
{
  const int n = 100;
  int a[n];

  for ( int i = 0; i < n; i++ )
    a[i] = i;

  std::random_shuffle ( a, a + n );

  for ( int i = 0; i < 4; i++ )
    std::cout<< a[i] <<'\n';
}

For the former, you would want to change the value of n to 101 so that 100 is included in the range.

You should revisit definition the of rand() and srand() functions,
both will give you non-repeating random numbers from 0 to your system's 'randmax' value.
Just use the standard form of rand() with a 'shift' and 'scale factor',eg:
number = shift value + rand() % scaling factor;
// shift value = minimum number
// scale factor = repeat point (What ever the modulus is chosen to be.)
For your code, simply populate your array with the numbers returned from the computation.
If your for loop conitunation limit is greater than the (shift value + scale factor), then the numbers will repeat in that range.

Try to keep your code simple, as you don't really need '2' for loops and all those 'if' tests.
Nice try just the same.

>both will give you non-repeating random numbers from 0 to your system's 'randmax' value.
Sorry, but that's incorrect. First, srand doesn't give you random numbers at all, it seeds the generator for rand to give you random numbers. Second, rand is not only allowed to repeat numbers, the very definition of random numbers makes repeating values expected.

Your solution doesn't address the problem, which is, for example, in the range of [0,100) the value 50 may appear more than once.

Your easiest bet is to do a random shuffle of the range:

#include <algorithm>
#include <iostream>

int main()
{
  const int n = 4;
  int a[n];

  for ( int i = 0; i < n; i++ )
    a[i] = i;

  std::random_shuffle ( a, a + n );

  for ( int i = 0; i < n; i++ )
    std::cout<< a[i] <<'\n';
}

yes, [0,100].
So, could I just modify it like the following?
std::random_shuffle ( a, a + 101 );

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.