Results 1 to 6 of 6

Thread: Picking from a text file at random?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312

    Picking from a text file at random?

    Im tryign to make my program (a trivia one) read from a text file, well, it reads the questions from the text file.

    But im trying to make it pick the questions at random. I cant find any tutorials for this, so wondered if anyone here could help..

    thanx
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  2. #2
    jim mcnamara
    Guest
    Code:
    #include <stdio.h>
    void main(void){
       char arr[100][100];  // one hundred questions
       char tmp[100];
       int i,j;
       int q=(-1);
       FILE*in;
       in = fopen("myquestions.txt","r");
       if (ferror(in) || in ==NULL){
                 perror("Error opening input file");
                 exit(1);
       }
        memset (tmp,'\0',sizeof(tmp) );
       while (!feof(in) ){
                if(fgets(tmp,99,in)!=NULL) {
                      q++; 
                      strcpy(arr(q),tmp);   
                }
        }          // you now have a char array of length q
         fclose(in);
         for (i=0;i<10;i++) { // ask 10 questions 
               j = (int) ( ( (float)rand()/ (float)RAND_MAX ) *(float)(q +1) ) ;
              // j is a random value between  zero and q
               printf("question: %s ?\n", arr[j]);
         }          
    }

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    thnaks
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    j = (int) ( ( (float)rand()/ (float)RAND_MAX ) *(float)(q +1) ) ;
    what is the advantage of this (it's awfully slow...)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    jim mcnamara
    Guest
    There is no great advantage to this, other than eliminating the use of more variables declared as float.

    You're right - casting is very slow - but this app probably does one calculation like this every 30 seconds. It isn't in a loop executing at full cpu speed. If it were it would be a terrible choice.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I mean what is the advantage of this vs
    int i = rand() % q;
    ?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width