more tic tac toe

Started by
30 comments, last by alvaro 11 years, 5 months ago

is there anyway I can generate a random number only once and do not have it repeat.

If you mean produce a sequence of numbers where no number is repeated, like 3, 1, 9, 5, 2, 4, ... then sure, it's very easy:

1. generate an array [0, 1, 2, 3, 4, ... as much as you need, pretty much put the numbers you want in there]
2. for each element A in the array...
----> select a random element B in the array
----> swap elements A and B
3. iterate through the modified array to get your repeat-free random numbers

See Fisher-Yates shuffle. Is this what you meant?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Advertisement
yes thanks
Note the algorithm I posted is the naive, inefficient one. The correct one is given on Wikipedia and is much faster:

[source lang="plain"]To shuffle an array a of n elements (indices 0..n-1):
for i from n ? 1 downto 1 do
j ? random integer with 0 ? j ? i
exchange a[j] and a[/source]

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Also, 0 is counted as a space so in an 8 space array there is in fact 9 spaces.


How can there be 9 spaces in an array of size 8? That makes no sense.

Arkane7 was correct. You will need to change that 8 to a 9. Otherwise, you can change the < sign to <= .
well i have found an algorithm that might work

#include <iostream>
#include <ctime>
using namespace std;
bool isContained(int arr[],int n,int x)
{
for(int i=0;i<n;i++)
{
if(arr==x)
return true;
}
return false;
}
int main()
{
srand(time(0));
const int x = 100;
int r[x];
for(int i=0;i<x;i++)
{
r=1+rand()%x;
while(isContained(r,i,r))
{
r=1+rand()%x;
}
}
for(int i=0;i<x;i++)
cout<<r<<"\n";
cout<<"\n\n";
return 0;
}
That is slow. I gave a link to the proper algorithm in my last post - why not use it? Since you want code:

[source lang="cpp"]#include < iostream>
#include < cstdlib>
#include < ctime>

using namespace std;

void fisherYates(int array[], int length)
{
/* Go over each element backwards. */
for (int t = length - 1; t != 0; t--)
{
/* Select random element in [0..t] */
int r = rand() % (t + 1);

/* Swap them. */
int tmp = array[t];
array[t] = array[r];
array[r] = tmp;
}
}

int main()
{
/* Randomize. */
srand(time(0));

/* Prepare array of N elements. */
const int length = 20;
int array[length];

/* Initialize it. */
for (int t = 0; t < length; ++t) array[t] = t;

/* Shuffle. */
fisherYates(array, length);

/* Print out array. */
for (int t = 0; t < length; ++t) cout < < array[t] < < " ";
cout < < endl;
}[/source]

(remove spaces between << as the source tags munch'em apparently)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

cool thanks
[sub]

I have tried fisherYates shuffle but it still does not work.here is my code[/sub]

[sub]

void Computer::move_player_O()
{
srand(time(NULL)); [/sub]
[sub]

const int length=10;
int array[length];
for(int t=1;t<=9;t++)
array[t]=t;
fisherYates(array,length);
player_O=array[t];
t++;
}[/sub]
[sub]

[/sub]
[sub]

void Computer::check_player_O()
{
board[player_O]='O';
}[/sub]

Why are you incrementing t again inside the loop... the t++ inside the for construct already takes care of that :(

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

ok i took the t++ out but it still does not work,I am so close to fihishing,any more help would be great.

This topic is closed to new replies.

Advertisement