I want to create 7 different numbers between 1 - 50 and the numbers cannot match any of the other numbers, this is what I have so far, is there an easier / cleaner way to write it or is my way fine?
One way is to generate a list of whatever items you're randomizing. Then, instead of straight up rand()%50, you randomize a position in the list and remove the item at that location.
Hmm I actually thought of a much better way to do it (I think) would something like this work?
1 2 3 4 5 6 7 8
int rng[6]; //= {rand() % 50};
for (int i = 0; i < 6; i++) {
rng[i] = rand() % 50;
if (rng[i] == rng[0] || rng[i] == rng[1] || rng[i] == rng[2] || rng[i] == rng[3] || rng[i] == rng[4] || rng[i] == rng[5] || rng[i] == rng[6]){ //Trying to make this re-randomise another number here if its equal to any of the others
rng[i] = rand() % 50;
}
}
Hmm I actually thought of a much better way to do it (I think) would something like this work?
No. Six iterations and it's done no matter what values rng contains. It also treats all rng elements as if they were initialized, even though they were not.
I guess I will just stick with what I have then, I was just trying to make it as clean and small as possible preferably within a small loop but doesn't look possible.
Although your ways works cire (thanks for the time and effort!) its a little complex using features/functions I am not familiar with and as I am just trying to nock out the program as quick as possible for my own use, I guess I will stick with the raw basics I have learned until its completed and can spend alot of time really tidying up / optimizing.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
// nValues is the number of unique values to generate.
// vals points to an array large enough to hold nValues numbers.
// min is the smallest number in the range to generate numbers for.
// max is the largest number in the range to generate numbers for.
// IMPORTANT: max-min cannot exceed 255.
void generateUniqueRandomValues(unsigned nValues, unsigned* vals, unsigned min, unsigned max)
{
using std::swap;
using std::rand;
constunsigned maxElements = 256 ; // some arbitrary value.
unsigned values[maxElements];
constunsigned valuesSize = max - min + 1; // the number of elements of the values array which we will use.
for (unsigned i = 0; i < valuesSize; ++i) // fill values with numbers from min to max.
values[i] = min + i;
for (unsigned i = 0; i < nValues; ++i)
{
constunsigned valuesLeft = valuesSize - i;
constunsigned index = rand() % valuesLeft; // pick a random index for an unused value.
vals[i] = values[index]; // store the value in the user-supplied array.
swap(values[index], values[valuesLeft-1]); // swap the used value with the last unused value.
}
}
int main()
{
usingnamespace std;
srand(time(0));
constunsigned nVals = 7;
constunsigned minVal = 1;
constunsigned maxVal = 50;
unsigned randValues[nVals];
generateUniqueRandomValues(nVals, randValues, minVal, maxVal);
for (unsigned i = 0; i < nVals; ++i)
cout << "#" << i+1 << ": " << randValues[i] << '\n' ;
}