Thread: random strings?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    11

    Question random strings?

    I cant figure out how to get a random string from an array, can someone please help me. I put my code below.

    thanx

    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
    string allcards[52] = {"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", "4c", "2d", "4h", "4s", "5c", "5d", "5h", "5s", "6c", "6d", "6h", "6s", "7c", "7d", "7h", "7s", "8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s", "10c", "10d", "10h", "10s", "Jc", "Jd", "Jh", "Js", "Qc", "Qd", "Qh", "Qs", "Kc", "Kd", "Kh", "Ks", "Ac", "Ad", "Ah", "As"};
    srand(time(NULL));
    string card = rand(allcards);
    cout << card;
    system("PAUSE");
    return 0;
    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    string card = allcards[ (rand() % 52) ];
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    try
    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        string allcards[52] = {"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", 
                               "4c", "4d", "4h", "4s", "5c", "5d", "5h", "5s", 
                               "6c", "6d", "6h", "6s", "7c", "7d", "7h", "7s", 
                               "8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s", 
                               "10c", "10d", "10h", "10s", "Jc", "Jd", "Jh", "Js", 
                               "Qc", "Qd", "Qh", "Qs", "Kc", "Kd", "Kh", "Ks", 
                               "Ac", "Ad", "Ah", "As"};
        srand(time(NULL)); 
        int cardnum = rand() % 52;
        string card = allcards[cardnum]; 
        cout << card;
    
        return 0;
    }
    rand() returns a value from 0 to 32767, not a string
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    135

    Re: random strings?

    Originally posted by Bobit
    I cant figure out how to get a random string from an array, can someone please help me. I put my code below.

    thanx

    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    #include <stdlib.h>
    You only need one of <cstdlib> and <stdlib.h>, preferably the former.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  2. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. Random Sequences
    By zowen in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 01:58 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM