Display Random Quotes 
Author Message
 Display Random Quotes

Hey Nick:

Quote:

>Could any one give advice ( or source code) for a program that will
>randomly display a quote each time the program is run. i.e running
>"quote.exe". The program only needs to hold five quotes.

An easy way (the way my QUOTE.EXE works) is to use Randomize and Random
(n) to generate the random number (for 5 quotes, use Random (5), and it
returns a value 0..4), and then a case statement to evaluate the number
and display the quote.

Best wishes,

John Siirola



Wed, 18 Jun 1902 08:00:00 GMT  
 Display Random Quotes
Could any one give advice ( or source code) for a program that will
randomly display a quote each time the program is run. i.e running
"quote.exe". The program only needs to hold five quotes.

Thanks
--
Nick Powell



Wed, 18 Jun 1902 08:00:00 GMT  
 Display Random Quotes

Quote:

>Could any one give advice ( or source code) for a program that will
>randomly display a quote each time the program is run.
>Nick Powell

Nick,

Just happen to have written the very thing.
It may not be the most elegant piece of code on the Net but it does the
trick.

Program RandomQuote;

{

Written

       By:        DEAN STRINGER aka the flying kiwi.
       Date:      Dec '95

         Hamilton
Aim

   To display a logon message picked at random from a text file.

Method:

   Reads through the entire file <QuoteFileName> and increments a counter
   for every occurance of <QuoteHeader>.
   Then resets the file and generates random number <TargetCount> in range
   1-<Count> and passes through file again, stopping after <TargetCount>
   occurances of <QuoteHeader> and displays all lines to CRT until the
   next <QuoteHeader> or EOF marker is found.

Limitations:

   The Prog only carries out farly basic file checking. If any DOS file
   error occurs (eg File Not Found, Directory Not found), when the prog  
   tries to open the <QuoteFile> in the dir pointed to by <QuoteFileDir>  
   then the executable will BOMB with msg..

     'Problem opening Quote File.....'
     'Please tell ',AdminName

   You must use CR/LF at end of each line to break up lengthy messages in
   the <QuoteFile> as the maximum string length is 255char.

Quote:
}

uses dos,crt;

Type

   String12 = String[12];

const

     AdminName='<<Your Name>>';
     QuotefileDir = '<<Put quote file path here>>';

     QuoteFileName = 'LOG_TIPS.TXT';

var

   Srec : SearchRec;

   InFile : text;

   FullFileName : string;
   tempStr : string;

   count, thiscount, targetcount : integer;

   pausechar : char;

BEGIN

randomize;

clrscr;

{ Check for existance of QuoteFile }

FullFileName:=QuoteFileDir+QuoteFileName;

FindFirst(FullFileName,AnyFile,Srec);

If DosError=0 then   { ie file exists }

   Begin

     Writeln('Tip of the day is..');

     Assign(InFile,FullFileName);
     Reset(InFile);

     Count:=0;

     Readln(Infile, TempStr);

     { count through quote file }

     While NOT EOF(InFile) do
           Begin
           If Copy(TempStr,1,2)=QuoteHeader then Count:=Count+1;
           Readln(Infile, TempStr);
           end;

     Reset(Infile);

     { pick one at random }

     targetcount:=random(count)+1;
     thiscount:=0;

     { Skip until random quote position }

     repeat

         If Copy(TempStr,1,2)=QuoteHeader then
              thiscount:=thiscount+1;

         Readln(Infile, TempStr);

     Until thiscount=targetcount;

     { Read and display quote, line by line till next quote
       marker found }

     Repeat

          Writeln(TempStr);
          readln(Infile,TempStr);


     If EOF(Infile) then Writeln(TempStr);

     { CLOSE FILES }

     Close(InFile);

   end

else

   begin

     Writeln('Problem opening Quote File.....');
     Writeln('Please tell ',AdminName);
     PauseChar:=readkey;

   end;
END.

Example Quote file contents....



Next quote......



Wed, 18 Jun 1902 08:00:00 GMT  
 Display Random Quotes

Quote:

>>Could any one give advice ( or source code) for a program that will
>>randomly display a quote each time the program is run.
>>Nick Powell

>Nick,

>Just happen to have written the very thing.
>It may not be the most elegant piece of code on the Net but it does the
>trick.

[Terribly inefficient source snipped]

This code reminded me of the bogosort algorithm.  For those who haven't
heard of this, it is essentially equivilant to taking a deck of cards
and throwing it up in the air until it falls down sorted.

I'd take a more direct approach as so:

open the file, determine the file length, pick a random number from zero
to filelength, position to that point, scan ahead (or backwards) to a
<cr> or <cr><lf>, then read the string.

Of course, with today's computing resourses, your `bogo-read' approach
(which I'd swear is the same approach Microsoft programmers
take) probably wouldn't amount to any discernable difference to the end
user for normal length files, but I'm from the era when you had to pack
your code into like 5K and run it on a 1 mhz machine.

Waste not, Want not.

--

finger or http://www.crl.com/~hoxsiew for PGP key



Wed, 18 Jun 1902 08:00:00 GMT  
 Display Random Quotes

Quote:

>Hey Nick:

>>Could any one give advice ( or source code) for a program that will
>>randomly display a quote each time the program is run. i.e running
>>"quote.exe". The program only needs to hold five quotes.
>An easy way (the way my QUOTE.EXE works) is to use Randomize and Random
>(n) to generate the random number (for 5 quotes, use Random (5), and it
>returns a value 0..4), and then a case statement to evaluate the number
>and display the quote.

That is one way, yes.

An alternate would be to compile (create) a list of your "quotes" into
a single file, then put the "count" of all the quotes in the
beginning, so if you have 275 different quotes, then the first line
would be

275

That way, you can make your Randomizing section something like :
Randomize;
QuoteNumber := 1 + Random( Total_Quotes );  

where Total_Quotes was the number read from the first line of the
file.

Then you can read to that position in the file, get that quote and
print it, close the file and end.

Of course sequentially reading each and every byte until then would be
potentially time-intensive... so you could make the file a
random-access file and use Block Reads or whatever you want.  For a
single quote, it won't make a difference really unless there are
several hundred quotes or you are reading from a floppy disk.

Tim...



Wed, 18 Jun 1902 08:00:00 GMT  
 Display Random Quotes


Quote:

> >Hey Nick:


> >>Could any one give advice ( or source code) for a program that will
> >>randomly display a quote each time the program is run. i.e running
> >>"quote.exe". The program only needs to hold five quotes.

Hmm, sounds like my `Tomsystems Quote' which, hopefully <g>, has put a
random quote on the end of my mail :)

Quote:
> >An easy way (the way my QUOTE.EXE works) is to use Randomize and Random
> >(n) to generate the random number (for 5 quotes, use Random (5), and it
> >returns a value 0..4), and then a case statement to evaluate the number
> >and display the quote.

> That is one way, yes.

> An alternate would be to compile (create) a list of your "quotes" into
> a single file, then put the "count" of all the quotes in the
> beginning, so if you have 275 different quotes, then the first line
> would be

> 275

> That way, you can make your Randomizing section something like :
> Randomize;
> QuoteNumber := 1 + Random( Total_Quotes );  

> where Total_Quotes was the number read from the first line of the
> file.

> Then you can read to that position in the file, get that quote and
> print it, close the file and end.

Remember that Quotes are of varying length.

Quote:

> Of course sequentially reading each and every byte until then would be
> potentially time-intensive... so you could make the file a
> random-access file and use Block Reads or whatever you want.  For a
> single quote, it won't make a difference really unless there are
> several hundred quotes or you are reading from a floppy disk.

TQ works by creating an index file of the Quote file.  My index files contain
other information such as the path name of the Quote file (My quote files are
scattered over my hard disk, but the indexes are in one dir), a Title for
the Quote and finally the start position and length of each Quote.

v1.0 is available with source at:

ftp://ftp.demon.co.uk/pub/ibmpc/dos/apps/quotes/tq/tq10.zip

The Source code, however is _awful_.  It is one of the first programs I wrote
in Pascal and I still didn't know the language very well. (I wrote my own
version of the BASIC Mid$...Hmm, 'Copy'? what's that?!)

The only nice source code is the rewritten TQADD 1.2, which is not available yet
(The program that adds to mail).

The one thing which separates TQ from the rest is that it can handle plain
text files as Quote files, without the need for '%'+#10 to delimit Quotes.

--



Wed, 18 Jun 1902 08:00:00 GMT  
 Display Random Quotes

Quote:

>Could any one give advice ( or source code) for a program that will
>randomly display a quote each time the program is run. i.e running
>"quote.exe". The program only needs to hold five quotes.

Umm yer ... create a text file with your quotes in it (probably best
to have one per line), if you want to save time, specify on the top
line how many quotes you have.

Open file.
Read how many quotes.
Use the RANDOM(integer):integer function passing the number of quotes
to it.
Read through the file or the number of lines that's returned by the
RANDOM function.
That's the quote!.

That's the basic pseudo code, if you have problems coding .. write
back.

I did soemthing similar for windows where I've got a directory of
pitures (mostly in JPG form, but not necessarily) where when I start
windows it runs the program which randomly chooses a picture from the
directory, converts it to a bitmap in C:\WINDOWS\WINBG.BMP.
My Windows background is set to that default file.

Brian Tombleson



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Random isn't random?

2. Random where no random should be!

3. Random isn't random?

4. Help : Single Quotes!

5. Good Quotes (zjxahm)

6. Quote in sql statement with Oracle

7. single or double quote with tquery

8. Semicolons and quotes with Sybase/Oracle queries

9. need expert's Help: single quote '

10. Single quotes in Delphi

11. Problems with filter and quotes

12. Good Quotes (hffehz)

 

 
Powered by phpBB® Forum Software