Author | Message | Time |
---|---|---|
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. | October 17, 2003, 10:34 PM |
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! | October 17, 2003, 10:36 PM |
Dyndrilliac | Thanks! | October 17, 2003, 10:38 PM |
Freeware | if you need an example: (this is done with a listbox not an array) [code] 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 [/code] Then to get a quote: [code] 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 [/code] There you have it good luck. | October 21, 2003, 11:26 PM |
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! | October 22, 2003, 12:45 AM |
St0rm.iD | I've found Collections to be one of the best things in VB. | October 22, 2003, 1:52 AM |
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. | October 22, 2003, 7:55 AM |
Freeware | [quote author=iago link=board=17;threadid=3129;start=0#msg24924 date=1066783551] 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! [/quote] 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) | October 22, 2003, 11:02 PM |