shuffling arrays?

This is a discussion on shuffling arrays? within the C++ Programming forums, part of the General Programming Boards category; How would you shuffle an array? Like say we have an array- Code: int array[4] = { 1, 2, 3, ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Cool shuffling arrays?

    How would you shuffle an array? Like say we have an array-
    Code:
    int array[4] = { 1, 2, 3, 4, 5 }
    How would I mix up the numbers so that I have it randomly mixed up-
    Code:
    { 2, 4, 5, 1, 3 }
    Could I use something like XOR?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How would I mix up the numbers so that I have it randomly mixed up
    There is a function called random_shuffle() in <algorithm>.
    Code:
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
      int array[5] = { 1, 2, 3, 4, 5 };
    
      std::random_shuffle ( array, array + 5 );
    
      for ( int i = 0; i < 5; i++ )
        std::cout<< array[i] <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 08:34 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21