Thread: Random Sequence + Time ?

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

    Random Sequence + Time ?

    Hi everyone,
    I got a piece of C code to "evolve"(Text wants me to create another file with the same structure but it's another thing)-in reality it's the past "went-bad" exam.-
    I can't figure out how "srand" works with time in this code?
    I repeat that I got a book where I study,but I already tried once this exam and everytime new functions appears.
    I go up looking in the book and with ctrl+f = srand appears only 1 time near to int rand(void).
    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 numbers[ARRLEN];
        char name1[STRINGLEN+1];
        char name2[STRINGLEN+1];
      }
      t_datum;
    
    
    int main()
    {
      FILE *f;
      t_datum x;
      int i,k,n,temp;
      srand(time(0));                            //   <-- WHAT'S THIS?
      n = MAXREC + rand() % MAXREC;
      if ((f = fopen("archive","wb")))
      {    
        for (k = 0; k < n; k++)
        {
          for (i=0; i<ARRLEN; i++)
            x.numbers[i]=rand()%10000;
          for (i=0;i < STRINGLEN; i++)
          {
            temp=rand()%8;
            if (temp==0)  
              x.name1[i]= 32;
            else  
              x.name1[i]= 65 + rand()%26;
          }
          x.name1[STRINGLEN]='\0';
          for (i=0;i < STRINGLEN; i++)
          {    
            temp=rand()%8;
            if (temp==0)  
              x.name2[i]= 32;
            else  
              x.name2[i]= 97 + rand()%26;
          }      
          x.name2[STRINGLEN]='\0';
          fwrite(&x,sizeof(t_datum),1,f);
        }
        fclose(f);
      }
      else
        printf("Error opening the file.\n");
      return 0;
    }
    Ps. I have tomorrow the next C test and maybe I'll be willing to go if i can solve at least a part of this test...if you want the whole text requests for what to do tell me and I'll write the full text.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Thanks for spam
    See your other urgency thread.
    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 without duplicates
    By carlosmarin7 in forum C Programming
    Replies: 4
    Last Post: 08-09-2010, 11:20 PM
  2. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 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