Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,116 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!




Random out 10 number in 52 number ..

 
Reply to this topicStart new topic

Random out 10 number in 52 number ..

johnchan8888
2 Apr, 2007 - 01:05 AM
Post #1

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
Random out 10 number in 52 number ..

But how can I make sure the number not gen. as follow such as 0,1,2,3,4,5,6,7,8,9

Or some of the number is 5,6….. 50,51

I would like to make sure that the number don’t gen. out as I mention .

Many many thanks!
User is offlineProfile CardPM
+Quote Post

horace
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 01:13 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
not sure what you problem is
CODE

rand() % 53

will give a pseudo random number 0 to 52 inclusive.
You can use time() to see the generator so each time you run you get a different sequence.
User is offlineProfile CardPM
+Quote Post

johnchan8888
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 01:34 AM
Post #3

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
QUOTE(horace @ 2 Apr, 2007 - 02:13 AM) *

not sure what you problem is
CODE

rand() % 53

will give a pseudo random number 0 to 52 inclusive.
You can use time() to see the generator so each time you run you get a different sequence.


thx!~~
My problem that is i would like to Generate out 10 number .. between 0 and 52. Then I would like to make sure the generated out is (not)
e.g.
0,1,2,3,4,5,6,7,8,9
or
10,11,12,13,14,15,16,17,18,19
etc/

I would like to make sure the radom number don't less or more than the coming up number.

User is offlineProfile CardPM
+Quote Post

horace
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 01:51 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
a loop
CODE

    for (i=0; i<10; i++)
       printf("%d\n", rand() % 53);


gave
8
38
4
44
45
50
48
28
8
45

do you need to check if the same number occurs more than once?
User is offlineProfile CardPM
+Quote Post

johnchan8888
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 02:02 AM
Post #5

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
QUOTE(horace @ 2 Apr, 2007 - 02:51 AM) *

a loop
CODE

    for (i=0; i<10; i++)
       printf("%d\n", rand() % 53);


gave
8
38
4
44
45
50
48
28
8
45

do you need to check if the same number occurs more than once?


yeah~~
thanks so much!! /// yes, can it check the same number occurs more than once ?... that's great!!!

User is offlineProfile CardPM
+Quote Post

johnchan8888
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 02:15 AM
Post #6

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
QUOTE(johnchan8888 @ 2 Apr, 2007 - 03:02 AM) *

QUOTE(horace @ 2 Apr, 2007 - 02:51 AM) *

a loop
CODE

    for (i=0; i<10; i++)
       printf("%d\n", rand() % 53);


gave
8
38
4
44
45
50
48
28
8
45

do you need to check if the same number occurs more than once?


yeah~~
thanks so much!! /// yes, can it check the same number occurs more than once ?... that's great!!!


but unfortunately
i try this code
then the output is
41
23
27
0
36<--
36<--
30
49
38
31
User is offlineProfile CardPM
+Quote Post

horace
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 02:21 AM
Post #7

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
if you need to check that a number does not occure more than once you can put the numbers into an array - get the next random number check thru the array if it is not a duplicate add it to the array otherwise ignore it.

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Random Out 10 Number In 52 Number ..
2 Apr, 2007 - 04:15 PM
Post #8

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Say I had a deck of cards and I wanted to shuffle them. Well lets say I put the numbers 0-51 into a 52 element array (cards[]). Then I loop though the array (using 'count' as a counter) selecting a random number (rnd) between 0 and 51, now I swap(cards[count], cards[count]). I may choose to loop though the deck of cards more than once to get a good shuffle.

The array now contains the numbers 0-52 arranged in a fairly unpredicatable manner. This method may not be the most secure for online gamming (though you would be surprized at how faulty some of those systems are), but it will make for an ok game of solitare.

The above is a cheap version of the Fisher-Yates or Knuth shuffle. There are other and even some better algorithms, but this is the model.

[EDIT] See snippet

This post has been edited by NickDMax: 2 Apr, 2007 - 08:28 PM
User is offlineProfile CardPM
+Quote Post

johnchan8888
RE: Random Out 10 Number In 52 Number ..
8 Apr, 2007 - 12:06 AM
Post #9

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
thx!

CODE

void KnuthShuffle(int* pArr)
{
    int rand;
    for(int i=51;i>=0;i--)
    {
        rand=GenRand(0,i);
        swap(pArr[i], pArr[rand]);
    }
}


may i ask that. do i need to define the "swap".... as a int or something?

or i need to include some .h file ?

because i run this program , it has some error on swap ..
Thanks so much!!!
User is offlineProfile CardPM
+Quote Post

johnchan8888
RE: Random Out 10 Number In 52 Number ..
8 Apr, 2007 - 01:42 AM
Post #10

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
CODE
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <cstdlib>
#include <ctime>
int i,a[53];


int main()
{
    srand( time(0));//seed of the random timer

    for (i=0; i<20; i++)
    {
    if (i==0)    
        a[i]=printf("%d\n", rand() % 53);
    else{
            bool check = false;
            while(check==false)
            {
                if (a[i-1]==a[i])
                {
                    a[i]=printf("%d\n", rand() % 53);
                    check=false;
                }
                else
                {    
                    check=true;
                }
            }
        }
    }


    


} // end main

I'm trying to generate 10 radom number and i should make sure the number don't duplicate. but in my program, the upper one, it's still have opportunity to generate our duplicate number..~

Would any body can give me some hint ?
Many thanks!!!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Random Out 10 Number In 52 Number ..
9 Apr, 2007 - 02:05 AM
Post #11

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
QUOTE(johnchan8888 @ 8 Apr, 2007 - 01:06 AM) *

thx!

CODE

void KnuthShuffle(int* pArr)
{
    int rand;
    for(int i=51;i>=0;i--)
    {
        rand=GenRand(0,i);
        swap(pArr[i], pArr[rand]);
    }
}


may i ask that. do i need to define the "swap".... as a int or something?

or i need to include some .h file ?

because i run this program , it has some error on swap ..
Thanks so much!!!



swap is a function. It will swap the values for two of the array elements integers. You could write your own swap routine, use one of the ones listed in the snippets or just add #include <algorithm> to your code. (it is really just easy to just write a quick swap routine, and if this is for a class teachers don't ussualy like it if you use STL solutions).


As for your last post, my previous answer still holds.


Your code suffers from the problem that it does not check the entire array, it just makes sure that the last random number does not equal the current one. You would need to check the entire array:

CODE

<psuedo code>
int Counter;
int i;
for (Counter = 0; Counter<Max; Counter++)
{
    BadValue = true;
    while (BadValue)
    {
        value = GetRandom(1 to 52);
        BadValue=false;
        for (i=0; i<Counter;i++)
        {
            if (Array[i]==value)
            BadValue=true;
            break;
        }
    }
}
Of course there are better ways to do this, this is just an example. The Knuth version is really a good algorithm.
User is offlineProfile CardPM
+Quote Post

johnchan8888
RE: Random Out 10 Number In 52 Number ..
17 Apr, 2007 - 10:21 PM
Post #12

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 12


My Contributions
thx !! I'm now can use the following code to gen. out 52 number without duplicate ~!
but now i would to apply the these rand number into the GUI - cards
so that it can make my cards radom out in GUI...
I'm just have some idea to add a
CODE

img[0] = gcnew Bitmap("c:\\card\\1.png")
    img[1] = gcnew Bitmap("c:\\card\\2.png")
    img[2] = gcnew Bitmap("c:\\card\\3.png")
    img[3] = gcnew Bitmap("c:\\card\\4.png")
    img[4] = gcnew Bitmap("c:\\card\\5.png")
    img[5] = gcnew Bitmap("c:\\card\\6.png")
    img[6] = gcnew Bitmap("c:\\card\\7.png")
    img[7] = gcnew Bitmap("c:\\card\\8.png")
    img[8] = gcnew Bitmap("c:\\card\\9.png")
    img[9] = gcnew Bitmap("c:\\card\\10.png")
    img[10] = gcnew Bitmap("c:\\card\\11.png")
    img[11] = gcnew Bitmap("c:\\card\\12.png")
    img[12] = gcnew Bitmap("c:\\card\\13.png")
    img[13] = gcnew Bitmap("c:\\card\\14.png")
    img[14] = gcnew Bitmap("c:\\card\\15.png")
    img[15] = gcnew Bitmap("c:\\card\\16.png")
    img[16] = gcnew Bitmap("c:\\card\\17.png")
    img[17] = gcnew Bitmap("c:\\card\\18.png")
    img[18] = gcnew Bitmap("c:\\card\\19.png")
    img[19] = gcnew Bitmap("c:\\card\\20.png")
    img[20] = gcnew Bitmap("c:\\card\\21.png")
    img[21] = gcnew Bitmap("c:\\card\\22.png")
    img[22] = gcnew Bitmap("c:\\card\\23.png")
    img[23] = gcnew Bitmap("c:\\card\\24.png")
    img[24] = gcnew Bitmap("c:\\card\\25.png")
    img[25] = gcnew Bitmap("c:\\card\\26.png")
    img[26] = gcnew Bitmap("c:\\card\\27.png")
    img[27] = gcnew Bitmap("c:\\card\\28.png")
    img[28] = gcnew Bitmap("c:\\card\\29.png")
    img[29] = gcnew Bitmap("c:\\card\\30.png")
    img[30] = gcnew Bitmap("c:\\card\\31.png")
    img[31] = gcnew Bitmap("c:\\card\\32.png")
    img[32] = gcnew Bitmap("c:\\card\\33.png")
    img[33] = gcnew Bitmap("c:\\card\\34.png")
    img[34] = gcnew Bitmap("c:\\card\\35.png")
    img[35] = gcnew Bitmap("c:\\card\\36.png")
    img[36] = gcnew Bitmap("c:\\card\\37.png")
    img[37] = gcnew Bitmap("c:\\card\\38.png")
    img[38] = gcnew Bitmap("c:\\card\\39.png")
    img[39] = gcnew Bitmap("c:\\card\\40.png")
    img[40] = gcnew Bitmap("c:\\card\\41.png")
    img[41] = gcnew Bitmap("c:\\card\\42.png")
    img[42] = gcnew Bitmap("c:\\card\\43.png")
    img[43] = gcnew Bitmap("c:\\card\\44.png")
    img[44] = gcnew Bitmap("c:\\card\\45.png")
    img[45] = gcnew Bitmap("c:\\card\\46.png")
    img[46] = gcnew Bitmap("c:\\card\\47.png")
    img[47] = gcnew Bitmap("c:\\card\\48.png")
    img[48] = gcnew Bitmap("c:\\card\\49.png")
    img[49] = gcnew Bitmap("c:\\card\\50.png")
    img[50] = gcnew Bitmap("c:\\card\\51.png")
    img[51] = gcnew Bitmap("c:\\card\\52.png")


-- but i don't know how to apply these random number to gen out the photo in GUI.... thx!!
--------------------------------------------------------------------------------------------------------------------------
CODE

#include <iostream>
#include <cstdlib>   // for srand and rand
#include <ctime>     // for time
using namespace std;

int main() {
    int card[52];    // array of cards;
    int n;           // number of cards to deal
    srand(time(0));  // initialize seed "randomly"
    
    for (int i=0; i<52; i++) {
        card[i] = i;  // fill the array in order
    }
    
    while (cin >> n) {    
        //--- Shuffle elements by randomly exchanging each with one other.
        for (int i=0; i<(52-1); i++) {
            int r = i + (rand() % (52-i)); // Random remaining position.
            int temp = card[i]; card[i] = card[r]; card[r] = temp;
        }
        
        //--- Print first n cards as ints.
        for (int c=0; c<n; c++) {
            cout << card[c] << " ";  // Just print number
        }
        cout << endl;
    }
  
   return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:53PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month