Thread: String Array Shuffle

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    String Array Shuffle

    Alright so this my code for the shuffle so far. I am making a team randomizer with a max of 250 teams and it is 4 players per team.

    Code:
    	
    srand ( time(NULL) );
    for(i=0;i<(Teams*4);i++)
    {
    	int random = rand() % (Teams * 4);
    	strcpy(temp.x[0].name, player.x[i].name);
    	strcpy(player.x[i].name, player.x[random].name);
    	strcpy(player.x[random].name, temp.x[0].name);
    }
    
    for(i=0;i<Teams;i++)
    {
    	printf("-------------\nTeam %d\n--------------\n %s \n %s \n %s \n %s \n", i+1,player.x[i].name, player.x[i+1].name, player.x[i+2].name, player.x[i+3].name);
    }
    It shuffles good and all that, the only problem is that a player may be put down on the same team twice. I have been searching and searching and well being new, I have tried everything i can think of. So, any pointers

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    a player may be put down on the same team twice
    how is that not random?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Quote Originally Posted by nimitzhunter View Post
    how is that not random?
    What do you mean? I know what the code above does is random all the strings in the array, but i need it so that one string does not get randomized and put in two different locations. I dont know if i am explaining this right so.

    ex.

    array[0] gets randomized to array[8] , but i dont want it to also get randomized with say array[10] because when you print out the list, a name that was entered will be gone and two of the same name will appear.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Nevermind, should of not printed so stupidly lol...
    Last edited by omGeeK; 02-14-2011 at 07:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM