![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 2
| Restricting occurrence of same number in random number generation 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 | |
| | #2 |
| C++ Witch 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 | |
| | #3 |
| Registered User Join Date: Oct 2009
Posts: 2
| how do i do a random shuffle |
| teenvista is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ 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 | |
| | #5 |
| Wheres the lesbians? 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 | |
| | #6 |
| Rampaging 35 Stone Welsh 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 | |
![]() |
| Tags |
| random, random number generation |
| Thread Tools | |
| Display Modes | |
|
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 |