I'm trying to design an algorithm that will output data from an array in random order but not repeat until every peace of the data has been output. The size of the array is small (about 10).
it picks a random prime number smaller than the size of the array(lets call this number x) and picks a direction to move through the array( direction isn't necessary but increases randomness). the algorithm views the array as circular. from a random starting point it jumps to the to the next array location x away in the direction chosen.
Have a look. If you can find any errors, I would be grateful, to hear what you have to say.
Also if you have seen anything like this, I would love to check it out.
#include <iostream>
using namespace std;
class myArray
{
public: int getNext();
void ready();
myArray();
private:int size;
int index;//index for getNext
int primeNum;//prime number for getNext
int index2;//counts the number of times getNext is called
int direction;//direction multiplyer for getNext
int array[52];
};
myArray::myArray()
{
size = 52;
index = 0;
for( int i=0; i < size; i++)
{
array[i]=i;
}
}
void myArray::ready()
{
int count=0;
index2=0;
//generate a random prime number less than size
if(size >= 1)
{
count++;
if (size >= 2)
{
count++;
if(size >=3)
{
count++;
if(size >=5)
{
count++;
if(size >= 7)
{
count++;
if (size >= 11)
{
count++;
if(size >= 13)
{
count++;
}
}
}
}
}
}
}
count= (rand() % count) + 1; //count is set to a random number between 0 and its previous value
switch(count)
{
case 1:
primeNum = 1;
break;
case 2:
primeNum = 2;
break;
case 3:
primeNum = 3;
break;
case 4:
primeNum = 5;
break;
case 5:
primeNum = 7;
break;
case 6:
primeNum = 11;
break;
case 7:
primeNum = 13;
break;
default:
cout<<"error prime gen";
}
//set the multiplyer value of direction
//direction = 1 - (2 * (rand()%2));//give direction a value of 1 or -1
direction = 1;
}
int myArray::getNext()
{
int returnValue;
//indexed steps to the next index value
//using the primeNum and the direction to
//create the illusion of randomness
index = (index + (primeNum * direction));
index2++;
if (!((index2*primeNum)%size))//this part is a bit confusing but it prevents index from steeping on top of its self
{
index= index + (1*direction);//every time index goes past 0 when index = index % size
// index gets incremented 1
}
index = index % size;
returnValue = array[index];
if(!(index2%52))
{
system("pause");
}
return returnValue;
}
int main()
{
srand(0);
myArray array;
array.ready();
for(int i =0; i <156; i++)//156 = 52 * 3
{
cout<<array.getNext()<<endl;
}
cout<<endl;
system("pause");
}