Thread: Randomization

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    14

    Randomization

    Will only p1 be randomized or will all the variables (p1,p2,q1,q2.............) be randomized.
    Code:
    srand((unsigned) time(NULL));
        p1 = rand%5;
        p2 = rand%5;
        q1 = rand%5;
        q2 = rand%5;
        r1 = rand%5;
        r2 = rand%5;
        s1 = rand%5;
        s2 = rand%5;
        t1 = rand%5;
        t2 = rand%5;
        u1 = rand%5;
        u2 = rand%5;
        w1 = rand%5;
        w2 = rand%5;

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Tough to answer since you haven't defined rand. If by "rand" you mean to be calling rand() for each variable, then yes, it will be randomized (albeit not necessarily very strongly). If by "rand" you mean to call rand() once and use it as a variable with the same value for each other variable, it will be the same for each usage.

    edit: Fixed your code tags for you.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    A general comment : They are pseudorandomized .

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    My exact question is will the line
    srand((unsigned) time(NULL));
    effectively randomize the seed values of all of the variables. Or do i have to write this line again and again.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You should read this tutorial: Generating Random Numbers in C and C++ - Cprogramming.com

    srand() seeds a random number generator. It merely sets up the system to give you random numbers - it doesn't actually generate even a single random number for you. Once you have called srand(), you can call rand() once for every random number you want to generate. So call srand() once no matter what, and then rand() once for each number. Again, in your code it looks like you're trying to use rand as a variable, not a function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomization in c++?
    By zanderela in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2008, 02:18 AM
  2. Randomization
    By ldb88 in forum C++ Programming
    Replies: 12
    Last Post: 06-21-2006, 12:36 AM
  3. Arrrays, Loops, and Randomization
    By myrddin in forum C Programming
    Replies: 4
    Last Post: 05-21-2003, 04:01 AM
  4. Randomization
    By blackmagic in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2002, 11:15 AM
  5. What is wrong with my type defination/randomization program?
    By Golden Bunny in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2002, 08:41 PM