Thread: Random Sequence + Time ?

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    21

    Random Sequence + Time ?

    Hi everyone
    I got a C code to "evolve"(Problem asks me to create another "database" or archive file,but this is another thing).-In reality this is the past "went-bad" exam.
    I can't understant how srand works and how it works with time..
    I got my book where I study, but searching with ctrl+f = srand appears only 1 time near "int rand(void)" explaining that starting with seed it creates new pseudo random numbers.
    But what has it to do with time,and the whole text?
    This is the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ARRLEN 30
    #define MAXREC 300
    #define STRINGLEN 20
    
    typedef
      struct
      {
        int numeri[ARRLEN];
        char nome1[STRINGLEN+1];
        char nome2[STRINGLEN+1];
      }
      t_dato;
    
    
    int main()
    {
      FILE *f;
      t_dato x;
      int i,k,n,temp;
      srand(time(0));
      n = MAXREC + rand() % MAXREC;
      if ((f = fopen("archivio","wb")))
      {    
        for (k = 0; k < n; k++)
        {
          for (i=0; i<ARRLEN; i++)
            x.numeri[i]=rand()%10000;
          for (i=0;i < STRINGLEN; i++)
          {
            temp=rand()%8;
            if (temp==0)  
              x.nome1[i]= 32;
            else  
              x.nome1[i]= 65 + rand()%26;
          }
          x.nome1[STRINGLEN]='\0';
          for (i=0;i < STRINGLEN; i++)
          {    
            temp=rand()%8;
            if (temp==0)  
              x.nome2[i]= 32;
            else  
              x.nome2[i]= 97 + rand()%26;
          }      
          x.nome2[STRINGLEN]='\0';
          fwrite(&x,sizeof(t_dato),1,f);
        }
        fclose(f);
      }
      else
        printf("Errore nell'apertura del file.\n");
      return 0;
    }
    Ps. I got this code because I already tried this test and I'll be willing to try tomorrow test if I can solve this past one..
    If you want the full test text,tell me and i will write it

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Imagine that your program begins with
    srand(0);

    Do you understand that without your explicit srand call that you always get the same answer.

    Calling time() just means you get different answers each time you run the program.

    Try also
    printf("Now=%ld\n",time ());
    And see how it changes from one run to another.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    21
    Now I understand! Thank you!
    2° question, if you can..do you maybe know what's that MAXREC? just an array?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Where do you see MAXREC near an array?

    > n = MAXREC + rand() % MAXREC;
    What does this do?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    21
    Quote Originally Posted by Salem View Post
    Where do you see MAXREC near an array?
    I think that's a macro like ARRLEN and STRINGLEN.

    n = MAXREC + rand() % MAXREC ;
    What does this do?
    That's what i'm asking

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Do you know the compiler sees this.
    n = 300 + rand() % 300;

    It's just math.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Sequence + Time ?
    By Alcatraz in forum C Programming
    Replies: 1
    Last Post: 09-12-2017, 06:01 AM
  2. random sequence without duplicates
    By carlosmarin7 in forum C Programming
    Replies: 4
    Last Post: 08-09-2010, 11:20 PM
  3. Help~~how to display a random sequence
    By wang8442 in forum C Programming
    Replies: 7
    Last Post: 12-05-2004, 09:45 AM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Replies: 3
    Last Post: 01-14-2002, 05:09 PM

Tags for this Thread