Jump to content

random phrase generator


Go to solution Solved by kylomas,

Recommended Posts

Here we go

Func RandomPhrase_From_KeyValue( $sIniFile )
;Read the Section Names
Local $asSection = IniReadSectionNames( $sIniFile )
If @error Then Return SetError(1, @error, -1 )
;Choose a Random Section Name
$asSection = $asSection[ _Random ( 1, $asSection[0] ) ]

;Now read the section
Local $asRet = IniReadSection( $sIniFile, $asSection )
If @error Then Return SetError(2, @error, -1 )
;Return a random value
Return $asRet[ _Random( 1, $asRet[0][0] ) ][1]

;This function returns the Max Value when the Min value is equal to it,
;By native function it returns zero which causes problem
Func _Random( $iMin, $iMax )
If $iMin = $iMax Then Return $iMax
Return Random( $iMin, $iMax, 1 )

;Our Test
Local $Phrase = RandomPhrase_From_KeyValue( "Test.ini" ) ;INI in ScriptDir
If @error Then Exit MsgBox( 16, "Error", "Code: " & @error ) <> -1
;Ask if you have any questions
MsgBox( 64, "Phrase", $Phrase )

#cs The INI file - Test.ini
1 = hello
2 = nice
3 = yes
xx = nothing
qwerty = keyboard
1_Phrase = Something
2_Phrase = Xcellence
First = Good
Second = Better
Third = Best

Ask for you have any queries

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Solution


I think the OP want a phrase consisting of a beginning, a middle and an end with each part containing a random selection appropriate to it's position in the phrase. There is a name for these kinds of things but it escapes me at the moment.


This might be a start

#include <Constants.au3>
#include <array.au3>

; create test INI file

FileDelete(@ScriptDir & '\phrase.ini')

Local $str
$str &= '00=The cow' & @LF
$str &= '01=I' & @LF
$str &= '02=He' & @LF
$str &= '03=She' & @LF
$str &= '04=They' & @LF
$str &= '05=All the girls' & @LF
$str &= '06=Most of the fish' & @LF
$str &= '07=A priest, a lawyer and a doctor' & @LF
$str &= '08=Some days' & @LF
$str &= '09=Sometimes' & @LF
$str &= '10=Whenever I' & @LF
$str &= '11=Whenever you' & @LF
$str &= '12=Whenever they' & @LF
IniWriteSection(@ScriptDir & '\phrase.ini', "Start", $str)
$str = ''
$str &= '01=were walking' & @LF
$str &= '02=sat on' & @LF
$str &= '03=said' & @LF
$str &= '04=asked' & @LF
$str &= '05=left for the day' & @LF
$str &= '06=were fishing' & @LF
$str &= '07=went to' & @LF
$str &= '08=came from' & @LF
$str &= '09=jumped on' & @LF
$str &= '10=jumped off' & @LF
IniWriteSection(@ScriptDir & '\phrase.ini', "Middle", $str)
$str = ''
$str &= '01=a boat' & @LF
$str &= '02=a house' & @LF
$str &= '03=OK' & @LF
$str &= '04=supper' & @LF
$str &= '05=school' & @LF
$str &= '06=church' & @LF
$str &= '07=home' & @LF
$str &= '08=hell' & @LF
$str &= '09=AutoIT' & @LF
$str &= '10=a tack' & @LF
$str &= '11=car' & @LF
IniWriteSection(@ScriptDir & '\phrase.ini', "End", $str)

; loop on a display of random phrases

Local $ret = 1

While $ret = 1
    $ret = MsgBox($mb_okcancel, 'My Random Phrase', _ini('Start') & ' ' & _ini('Middle') & ' ' & _ini('End'))

Func _ini($part)
    Local $aSect = IniReadSection(@ScriptDir & '\phrase.ini', $part)
    If @error = 1 Then
        MsgBox($mb_ok, 'INI Read Section Error', 'Press <ENTER> to exit')
    Return $aSect[Random(1, UBound($aSect, 1) - 1, 1)][1]
EndFunc   ;==>_ini

Note - For anything of significant size (you define significant) you might want to consider an SQLite solution (really not much more code than the above).


edit: corrected EOL for INI population. Changed @CRLF to @LF as extra lines were being generated

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...