Thread: srand(time(Null)): Any possibility to refine / tune the seed value

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    9

    srand(time(Null)): Any possibility to refine / tune the seed value

    Hi,

    I would like to understand the mechanism of the srand(..) random number generator.
    In the enclosed a small example. I would like to place 'X' character into all the position of the given 3x3 matrix randomly. If the selected position is occupied, the program will have to find a new empty position ( using the random generator )
    The random genenator works well till the 5th-6th position, but after that it couldn't find new empty place.

    Code:
    #include<stdio.h>
    #include<time.h>
    #include <stdlib.h>
    int main()
    {
        int row = 0,column = 0;
        int i;
        char table[3][3]= { {'?','?','?'}, {'?','?','?'}, {'?','?','?'} }; 
        int x=0;
        while (x<9)
        {
        
        do
            {
                srand(time(NULL) +100);
                row = rand() % 3;    
                srand(time(NULL));
                column = rand() % 3;
                printf("row: %d, column: %d\n", row, column);
            
            }
        while ( table[row][column]!='?' );
        table[row][column]='X';
        
        int j,k;
        for (j=0; j<3;j++)
        {
            for(k=0;k<3;k++)
             {
                 printf("%c\t", table[j][k]);
             }
             printf("\n");
        }
        
        printf("\n\n");
        getchar(); // demonstrate the table elements
        x++;
        
        }
    }

    I noticed, the random values didn't change so quickly. ( you can see it on the console if you run the code ). What would be the background of the srand() ? Do you have any idea how to refine the parameters in srand() fuction? Maybe, Shall I try it on a better processor?
    Very thx for your help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    srand should only be called once per run of the program to seed the pseudorandom number generator. It certainly should not be called before every call to rand: what you're doing is resetting the PRNG within the granularity of time(), hence you could end up generating the same number over and over again.
    Last edited by laserlight; 12-21-2018 at 04:57 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    104
    You could use the loop iterator to perform some arithmetic with the return value of rand(). That ought to spice things up a bit.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    You could use the loop iterator to perform some arithmetic with the return value of rand(). That ought to spice things up a bit.
    e.g
    Code:
        column = (rand() ^ x) % 3;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    That won't help: if the number generated is the same as before, doing arithmetic manipulation would just end up with the same final result.

    The overall strategy is poor though: if you're going to assign to all the positions and so only care about the order in which they are assigned 'X', then you're better off generating the numbers from 0 to 8 and then shuffling them, after which you follow the random order generated from the shuffle.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. srand time null
    By lmanukyan in forum C Programming
    Replies: 1
    Last Post: 02-01-2016, 07:47 AM
  2. Problem with srand(time(NULL))
    By xxx65536 in forum C Programming
    Replies: 2
    Last Post: 11-22-2011, 01:38 PM
  3. srand generates same number, no matter what seed
    By christianB in forum C Programming
    Replies: 5
    Last Post: 07-10-2011, 11:28 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  5. srand(time(NULL));
    By vin0_oce in forum C Programming
    Replies: 4
    Last Post: 09-28-2002, 08:34 PM

Tags for this Thread