Thread: Restricting occurrence of same number in random number generation

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Restricting occurrence of same number in random number generation

    hi,

    I use rand() to generate 2 numbers randomly(0 and 1). I want to restrict occurrence of 1 to 10.
    I cannot use search algorithm since this will remove all the 1's after 10 1's i want 1's to be randomly distributed across the array(9*9)

    Can anyone help me on this?
    Thanks
    teenvista

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Keep a count of 1s generated. Once the count hits 10, just fill in the rest with 0s.

    EDIT:
    Oh wait, that's wrong, since the distribution will not be uniform.

    Okay, here's a better try: fill in the array with ten 1s and the rest as 0s. Perform a random shuffle. You're done.
    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
    Oct 2009
    Posts
    2
    how do i do a random shuffle

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by teenvista
    how do i do a random shuffle
    Unfortunately there is no function available in the C standard library for performing a random shuffle. Search the web for "Fisher–Yates shuffle".
    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

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Heres an example of how you can do a random shuffle. It could be tweaked to do what you want.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        srand(time(NULL));
    
        int i, temp, swap_pos, array[52];
        for(i=0; i<52; i++)array[i] = i; //Init
        //Shuffle
        for(i=51; i>1; i--)
        {
            swap_pos = rand() % i;     //Pick random location to swap with
            temp = array[swap_pos];     //Swap elements
            array[swap_pos] = array[i];
            array[i] = temp;
        }
    
        for(i=0; i<52; i++) printf("%02d\n", array[i]);
        return 0;
    }

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    There are small set size shuffles that are deterministic, and large set shuffles that are non-deterministic. rand() can deterministically shuffle up to about 6 items, or non-deterministically shuffle about 30k items. For larger sets you need to implement some better RNG.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  4. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM
  5. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 08:04 PM

Tags for this Thread