Thread: This rand func is not working!!

  1. #1
    Registered User
    Join Date
    Oct 2007

    This rand func is not working!!

    thanks for checking this out:

    void computerMove(char board[SIZE][SIZE])
    	int x,y;
    	x = rand()%3;
    	y = rand()%3;
    	board[x][y] = 'x';
    this is just a simple random function for a computer to make a move in tic tac toe. Everytime i run the program i get the same randomly generated number [2][2]. I'm new to this so if it is blatantly obvious please make me feel retarded and then show me the light

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Call srand() first. You need to call it at least once at the beginning of your program to seed the random number generator. On some systems, you have to call it once per thread. Don't worry about that for now. Somewhere near the start of your program, just put this:

    For what you're doing this should be sufficient. Make sure you include the proper header files.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You really should read the FAQ before posting. This is probably the most frequently asked question of all!

    Otherwise the poor FAQ writers will wonder why they even bothered...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I'm wondering...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM