Thread: random for loop

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    random for loop

    Hello everyone,

    When running a regulair for loop, the variable will increase by typically one, that would give

    1
    2
    3
    4
    5
    etc

    I am looking for a method of doing the same thing, but in random order.
    like:
    4
    2
    5
    1
    3
    etc

    I made a rather complex solution to this problem, my solution can be viewed here: C pastebin - collaborative debugging tool

    Im very sure that there is a smarter and less cpu-demanding way to do this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you want to create an array of these numbers, and then shuffle the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Quote Originally Posted by laserlight View Post
    It sounds like you want to create an array of these numbers, and then shuffle the array.
    i found this, which hopefully can aid me Ben Pfaff: How can I shuffle the contents of an array?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I've used an algorithm called the "Fischer-Yates" shuffle alot for randomizing arrays. It (or my version) works like this:

    - loop backward through the array, from the last element to the 3rd element, ie:
    Code:
    int i, len = /* array length */
    for (i=len; i>1; i--)
    - at each iteration, generate a random number (x) from 0 to the number of the current element (i):
    Code:
    x = rand()%i;
    - if x == i, continue, otherwise, swap element i with element x.

    That's about a half dozen lines. Remember: If you don't want the results to always be the same when you run the program, use srand(time(NULL)) once in main() before the shuffle is first executed.

    There's a slight imbalance in the algorithm in that elements at the end of the array have a greater chance of not being moved than elements at the beginning. If you wanted to resolve that, you could make x = rand()%len (but this actually decreases the overall randomness by creating the chance that previously shuffled elements could be swapped back) or run once forward and once backward. In practice, neither of those is necessary. Try it a few times -- the result is always a pretty thorough looking shuffle.
    Last edited by MK27; 01-26-2010 at 11:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Thank you very much for sharing that :-) ill try it out!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    loop backward through the array, from the last element to the 3rd element
    I think that to be more correct, you should actually loop to the 2nd element, i.e., when you reach the 2nd element, you are basically deciding whether or not to swap it with the first.

    Quote Originally Posted by MK27
    There's a slight imbalance in the algorithm in that elements at the end of the array have a greater chance of not being moved than elements at the beginning.
    I do not think so. Consider a four element array. The probability that the element at index 3 will be moved is the probability that it will be swapped with another element, i.e., the probability that index 3 will not be generated, which is 1 - 1/4 = 3/4.

    Now, the probability that the element at index 2 will be moved is the probability that it will be swapped with the element at index 3 (i.e., 1/4), and if it is not swapped with the element at index 3, then the probability that it will be swapped with an element before it in the array, i.e., 2/3. But this 2/3 is conditional on the element at index 3 not being swapped with this element, hence the probability is actually 1/4 + (3/4 * 2/3) = 3/4.

    If we examine the case for the element at index 1, we see that the probability that it will be moved is:
    1/4 + (3/4 * (1/3 + (2/3 * 1/2))) = 3/4
    (Assuming that you loop to the 2nd element.)

    (But I did not score very well for my probability module in university, so feel free to correct me if I am wrong )

    EDIT:
    Well, an empirical test shows that my analysis should be correct. Anyway, I noticed that although you wrote "3rd element", your code snippet has "i>1", so you are actually doing it right. One remaining problem would be that you talk about comparing if x == i, but you have to decrement i first for that to be useful.
    Last edited by laserlight; 01-26-2010 at 01:29 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

Tags for this Thread