Restricting occurrence of same number in random number generation

This is a discussion on Restricting occurrence of same number in random number generation within the C Programming forums, part of the General Programming Boards category; hi, I use rand() to generate 2 numbers randomly(0 and 1). I want to restrict occurrence of 1 to 10. ...

  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
    23,787
    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
    23,787
    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

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, 04: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, 07:04 PM

Tags for this Thread


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