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();
myArray();
private:int size;
int index;//index 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;
}
}

{
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:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
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;
for(int i =0; i <156; i++)//156 = 52 * 3
{
cout<<array.getNext()<<endl;
}
cout<<endl;

system("pause");
}``````

## All 3 Replies

Three of my four code snippets involve shuffling and random numbers without repeats. There is more than one way to do this. Here are my snippets:

So I assume that your code is misbehaving in some way? What is it doing that it shouldn't or what is it not doing that it should?

If your primeNum is a factor of the array size, you'll keep displaying the same values again and again as you go around the circle, skipping over most of the data.

that was a problem i was having when i first tried to get it to work. depending on the size of the array and the prime number chosen it would work only some of the time. ex. say the size of the array was 9 and the prime number was 3 and I started at 0. it would go like this: 0, 3, 6, 9(0),3,6,9(0)... skipping over most of the data like you said. but if the prime number was 5 again starting at 0 it would go like so: 0,5,10(1),6,11(2),7,12(3)... working just like i wanted it to.
So I put in the statement at line 103

``````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
}``````

it seems like its working in some way or another.
But I dont want to look at it till I finish my beer.

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.