troubles with random

This is a discussion on troubles with random within the C++ Programming forums, part of the General Programming Boards category; When i compile my program and then run it it picks the same number and letter for all of the ...

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

    Question troubles with random

    When i compile my program and then run it it picks the same number and letter for all of the randoms. If you know how i can generate different randoms will you please tell me.

    Thanx,

    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        cout << "jack = 11\n";
        cout << "queen = 12\n";
        cout << "king = 13\n";
        cout << "ace = 14\n";
        srand(time(NULL));
        int p = 2 + (rand() % 14);
        char suitp;
        srand(time(NULL));
        int numberp = 1 + (rand()%4);
        if (numberp == 1) { suitp = 'c'; }
        if (numberp == 2) { suitp = 'd'; }
        if (numberp == 3) { suitp = 'h'; }
        if (numberp == 4) { suitp = 's'; }
        srand(time(NULL));
        int o = 2 + (rand() % 14);
        char suito;
        srand(time(NULL));
        int numbero = 1 + (rand()%4);
        if (numbero == 1) { suito = 'c'; }
        if (numbero == 2) { suito = 'd'; }
        if (numbero == 3) { suito = 'h'; }
        if (numbero == 4) { suito = 's'; }
        srand(time(NULL));
        int k = 2 + (rand() % 14);
        char suitk;
        srand(time(NULL));
        int numberk = 1 + (rand()%4);
        if (numberk == 1) { suitk = 'c'; }
        if (numberk == 2) { suitk = 'd'; }
        if (numberk == 3) { suitk = 'h'; }
        if (numberk == 4) { suitk = 's'; }
        srand(time(NULL));
        int e = 2 + (rand() % 14);
        char suite;
        srand(time(NULL));
        int numbere = 1 + (rand()%4);
        if (numbere == 1) { suite = 'c'; }
        if (numbere == 2) { suite = 'd'; }
        if (numbere == 3) { suite = 'h'; }
        if (numbere == 4) { suite = 's'; }
        srand(time(NULL));
        int r = 2 + (rand() % 14);
        char suitr;
        srand(time(NULL));
        int numberr = 1 + (rand()%4);
        if (numberr == 1) { suitr = 'c'; }
        if (numberr == 2) { suitr = 'd'; }
        if (numberr == 3) { suitr = 'h'; }
        if (numberr == 4) { suitr = 's'; }
        cout << p << suitp << " " << o << suito << " " << k << suitk << " " << e << suite << " " << r << suitr << endl;
          system("PAUSE");
          return 0;
    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    use srand() only one time, at the beginning.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    only use srand(time(NULL)) once at the top of your program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 09:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 05:11 PM

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