C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-05-2009, 03:38 AM   #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
teenvista is offline   Reply With Quote
Old 10-05-2009, 03:41 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 10-05-2009, 03:44 AM   #3
Registered User
 
Join Date: Oct 2009
Posts: 2
how do i do a random shuffle
teenvista is offline   Reply With Quote
Old 10-05-2009, 03:47 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
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".
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 10-05-2009, 05:13 AM   #5
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
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;
}
__________________
Senior highbrow doctor of authority.
mike_g is offline   Reply With Quote
Old 10-05-2009, 10:21 AM   #6
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,929
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.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Reply

Tags
random, random number generation

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:27 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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