Create random array

This is a discussion on Create random array within the C++ Programming forums, part of the General Programming Boards category; Hello. Im looking for a good way to make a array in random order. But I really cant come up ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    Create random array

    Hello. Im looking for a good way to make a array in random order. But I really cant come up with anything good. Thought of something like this(sorry if syntax is wrong, but I guess you can see what Im trying to do):

    Code:
    // variable to set the number in correct place in second array
    int i = 0;
    
    // Create 1 array with values and one where we will make the numbers in random order
    int[3] numbers = {1,2,3};
    int[3] randomNum = {};
    
    // run as long as the last element in the second array is empty
    while(randomNum[2] == null){
       // take a random element from first array and put it into the second array
       int random = rand()%2;
       randomNum[i] = numbers[random];
       i++;
    }
    Just one problem here is that I might take same numbers from the first array many times. Maybe can be solved by setting the value taken from first array to "null" into the while{} somehow? Feels like a bad solution. Or? What do you think. Any tips?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For such purposes I find std::random_shuffle in <algorithm> useful.

    Code:
    #include <algorithm>
    
    int main()
    {
        const int MAX = 3;
        int numbers[MAX] = {1, 2, 3};
        std::random_shuffle(numbers, numbers + MAX);
        //values are now in random order in numbers
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Anon's solution is a good one.

    If you want to do this yourself, you will need to keep a list of which numbers you've taken (or perhaps, if that's more appropriate, remove the "taken" from the original list, e.g set numbers[random] = 0, and check the picking method to see if the number is zero, in which case you start again).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    Tons of thanks. That random_shuffle was very easy. Perfect for my purpose =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  2. Dynamically create an array?
    By Unknowntoyou000 in forum C++ Programming
    Replies: 16
    Last Post: 04-25-2008, 03:49 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Create array of CArray
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 09-30-2002, 11:22 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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