• Welcome to Valhalla Legends Archive.
 

Adding Random Quotes

Started by Dyndrilliac, October 17, 2003, 05:34 PM

Previous topic - Next topic

Dyndrilliac

Can someone give me a brief explanation pulling random lines of text from a text file for a quote function? I already have the system for adding quotes I just need to know how to go about showing them randomly, one at a time.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

iago

- Read the text file into an array, say string Quotes[]
- When you need a quote, pick a random number between 0 and the array size-1 (rnd() % Size), or however that's done in VB
- Print out that from the Quotes array, like Say(Quotes[RandNum]);
That's all there is to it.. good luck coding it!
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Dyndrilliac

Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Freeware

if you need an example: (this is done with a listbox not an array)


Public Function ReadQuoteFile(Filename as string, Listbox as Listbox) as Boolean

On Error Goto ReturnError
Dim intFile as Integer
Dim strInput as String

intFile = Freefile 'Get an open file number
Listbox.clear 'Clear all items
Open Filename for Input as #intFile 'Open the file
 Do While EOF(#intFile) = False 'Input till End of File
   Line Input #intFile, strInput 'Read 1 line of data
   Listbox.additem strInput 'Add to listbox
 Loop 'continue until end of file
Close #intFile 'close file

ReadQuoteFile = True 'No Errors
Exit Function

ErrorReturn:
ReadQuoteFile = False 'Error Accured
Close #intFile

End Function



Then to get a quote:



Public Function GetQuote(Listbox as Listbox, Optional QuoteNum as Integer = -1, Optional Random as Boolean = false) as String

If QuoteNum <> -1 then
'Use a pre-defined quote
GetQuote = listbox.list(quotenum)
Exit Function
end if

'Use a random quote
Dim intRandom as Integer
Randomize

intRandom = Int(Rnd * Listbox.listcount)
GetQuote = listbox.list(intRandom)

End Function



There you have it good luck.

iago

eew, using a list box is a bad++ way of doing it!

Incidentally, I think there's more merit in my response, he'll learn more by doing it himself with just having the algorithm!
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Banana fanna fo fanna

I've found Collections to be one of the best things in VB.

Adron

You could read the file once, writing down the location of the start of each quote as a long into a secondary file, an index file. Then you could generate a random number between 0 and the size of the secondary file divided by 4 and subtracted by 1. Then you could multiply that by 4, use it to seek a number in the index file, seek the corresponding position in the quote file, and use that quote.

If all your quotes are of similar length, you could just pick a random number of 0..lof(quote file), and seek forward/backward until you reach a quote boundary. That will not produce an even distribution of uneven length quotes though.

Freeware

Quote from: iago on October 21, 2003, 07:45 PM
eew, using a list box is a bad++ way of doing it!

Incidentally, I think there's more merit in my response, he'll learn more by doing it himself with just having the algorithm!

i completly agree with you, it just might be easier for him to use a listbox, if anybody cares I can upload the code using arrays.

and yes he will learn more by doing it himself, but these days all people care about is code (well at least the newbie programmers)