# Thread: Using random number function to change element of an array..

1. ## Using random number function to change element of an array..

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{

char board="....X....";
int match;
int testMoveNumber(); /* generates random number between 1 and 9 */

/* the section below numbers the match array keys to match the board arrays dots- the other moves will stay at zero so the computer can't select them */

if (board='.')
match=1;
else if (board='.')
match=2;
else if (board='.')
match=3;
else if (board='.')
match=4;
else if (board='.')
match=5;
else if (board='.')
match=6;
else if (board='.')
match=7;
else if (board='.')
match=8;
else if (board='.')
match=9;

/* section below matches the random number with an empty board spot and hopefully loops through with another number until it can write to the board again and break the loop */

int r=0;
while(r!=1) {

int a=testMoveNumber();

if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else if (match==a)
{board='0'; r++;}
else
testMoveNumber();
}

/* just printing to test the resultant random move by the computer */

printf("%c%c%c%c%c%c%c%c%c", board, board, board, board, board, board, board, board, board);

return 0;
}

int testMoveNumber()

{
const int lowestRandom = 1;
const int highestRandom = 9;

int randomNumber=-1;

time_t seconds;

time(&seconds);

srand((unsigned int) seconds);

randomNumber = rand() % (highestRandom - lowestRandom + 1) + lowestRandom;

return randomNumber;
}```

Hi everyone- I'm new to programming and am writing my first program. I am using what I have learnt so far to create a noughts and crosses game but I've got a bit stuck. I am having trouble writing the computers random move to the board array. Basically I'm trying to create a random number between 1 and 9- loop through the unselected board spaces that are numbered in the 'match' array, then writing the matching move back to the board array. I think it is because I am not understanding the regeneration of the random number to select an available space. I still don't know how to use pointers yet and it would be awesome at some point to have the program encapsulated in a few simple functions passing the array back and forth- but for now I'm building it this way I hope my description wasn't too confusing! many thanks, Joe 2. A few things:
*the sections between lines 17 and 34, and between 47 and 64, could and should be done with a loop. Have you covered loops yet?
*In line 66 you call testMoveNumber but you don't do anything with the result.
*Use = to set things to be equal to other things. Use == to check for equality. 3. Hi I called testMoveNumber again to try to generate a new random number for if the loop needs it? Is that wrong? 4. ^the second section is already in a while loop Popular pages Recent additions 