Sign in to follow this  
Followers 0
KRQ

Random from List

10 posts in this topic

#1 ·  Posted (edited)

Hi,

I have a list of words in txt file (one per line), and i want to randomly select once from them and put into a inputbox.

Edited by KRQ

Share this post


Link to post
Share on other sites



Use the function random. You can set a min and max value. So if you have 50 words you could use: Random(1,50)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Like,

$file = FileOpen("50 Lined.txt", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

    $50line = FileReadLine($file,Random(1,50,1)); $var to be displayed
        FileClose($file)

InputBox('Variable','Your Result',$50Line)
Edited by lordicast

[Cheeky]Comment[/Cheeky]

Share this post


Link to post
Share on other sites

Like,

$file = FileOpen("50 Lined.txt", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

    $50line = FileReadLine($file,Random(1,50,1)); $var to be displayed
        FileClose($file)

InputBox('Variable','Your Result',$50Line)

Thanks, nice. But this select one word when script is started.

I have:

Send($50Line)
Send("{ENTER}")

And this is repeat in my script. Now script choose one random word and entered every times.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks, nice. But this select one word when script is started.

I have:

Send($50Line)
Send("{ENTER}")

And this is repeat in my script. Now script choose one random word and entered every times.

You really have to try harder than that :) See how the example you were given works and apply it to what you want. Instead of just sending the line get another random line and send that. You could also consider _FileReadtoArray but probably not yet. Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I'm just a beginer in AutoIT :)

This is my actual script, is not working properly.

Main file:

#include <losowanie.au3>

$razy = 0
Do
    
Sleep(6000)

MouseClick("left", 312, 217, 1)
Send("^a")

Send($losowa_fraza)
Send("{ENTER}")

Sleep(6000)

MouseClick("left", 577, 489, 1)

Sleep(2000)

MouseClick("left", 575, 589, 1)

Sleep(4000)


MouseClick("left", 312, 217, 1)
Send("^a")


Sleep(1000)

Send($losowa_fraza)
Send("{ENTER}")

Sleep(1000)

MouseClick("left", 577, 489, 1)

Sleep(2000)

MouseClick("left", 575, 589, 1)



Until $razy = 1

losowanie.au3:

$plik = FileOpen("Frazy.txt", 0)


If $plik = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

    $losowa_fraza = FileReadLine($plik,Random(1,10,1)); $var to be displayed
    FileClose($plik)

In txt file, i have a 10 words, 1 per line. I want to random select 1 word from that 10 and entered them to the field. Now, script is entered each time the same word.

Edited by KRQ

Share this post


Link to post
Share on other sites

OK. Prolbem is resolved. Close pls.

Share this post


Link to post
Share on other sites

OK. Prolbem is resolved. Close pls.

I'm looking for making un script like your's. Can you post your code, please.

Thank you!


Mad Dog (Maurice) Vachon, a great Canadian professional wrestler!

Share this post


Link to post
Share on other sites

Here is my script. It works with the txt file "Pensées.txt" in the same folder of Pensées.au3.

Pensées.au3

Dim Const $nbligne = 10, $pausedepart = 3000, $pauselect = 5000, $laps = (15*60000), $finboucle = 10
Dim $compteurboucle = 0
Local $Ouvrefichier, $Lignealeatoire

$Ouvrefichier = FileOpen("Pensées.txt", 0)
If $Ouvrefichier = -1 Then
    MsgBox(0, "Erreur", "Impossibilité d'ouvrir le fichier 'Pensées.txt'.")
    Exit
EndIf
Sleep($pausedepart)

Do
    texteamontrer ()
    $compteurboucle = $compteurboucle + 1
    Sleep($laps)
Until $compteurboucle = $finboucle

FileClose($Ouvrefichier)
Exit


Func texteamontrer ()
    $Lignealeatoire = FileReadLine($Ouvrefichier,Random(1,$nbligne,1))
    SplashTextOn("Pensées", $Lignealeatoire & @CRLF & "Il est " & @Hour & ":" & @MIN & "." , "400", "200", "-1", "-1", 33, "Times New Roman", "20", "700")
        Sleep($pauselect)
    SplashOff()
EndFunc

Exemple of Pensées.txt

Bonjour mon ami!
Buongiorno caro mio!
Buenos dias amigo!
Good morning Dude!
Et la suite s'en vient
Fais comme tu peux, advienne que pourra
Dépêche-toi à partir si tu veux arriver
Pourquoi faire aujourd'hui ce que tu peux faire demain!
Ne remets pas à demain ce que tu peux faire aujourd'hui
Héhéhéhéhéhéhéhéhé!

Mad Dog (Maurice) Vachon, a great Canadian professional wrestler!

Share this post


Link to post
Share on other sites

Here is my new version. It's written for french users, but that works well all the same.

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.0.0
    Author:         MadDogVachon

    Script Function: Show some "pensée" from a txt file
    Script version: 1.1

#ce ----------------------------------------------------------------------------

#NoTrayIcon
$file = "Pensées.txt"

; Vérification of file existence and number of "pensées"
$filehandle = FileOpen($file, 0)
If $filehandle = -1 Then
    $number_of_pensees = 0
Else
    $how_many_lines = 0
    Do
        $how_many_lines = $how_many_lines + 1
        $number_of_pensees = $how_many_lines
        $how_many_linetest = FileReadLine($filehandle, $how_many_lines)
    Until @error = -1
    $number_of_pensees = $number_of_pensees - 1
EndIf
If $number_of_pensees = 0 Then
    MsgBox(16, "Erreur", "Impossibilité d'ouvrir le fichier " & $file & ".")
    Exit
EndIf

; Takes the time of reading, the automatic time of presentation and how many random "pensées" to show
; Maybe I will change with a dialog box with GUICtrlCreateUpdown. But it's not for now!
$title = "Petite pensée du moment"
$pausestart = 0
Do
    $readingpause = getnumfunc($title, "Inscrivez la durée en seconde de la pause automatique pour lire une pensée." & @CRLF & @CRLF & "  - Minimum de 10 secondes -", "10")
    If ($readingpause > 0 And $readingpause < 10) Then
        MsgBox(0, $title, "Il faut une durée égale ou supérieure à 10.", 10)
    EndIf
Until $readingpause > 9
$pause_between_pensees = getnumfunc($title, "Inscrivez la durée en minute du délais entre chaque pensée.", "0")
$pause_between_pensees = $pause_between_pensees * 60000
$end_of_loop = getnumfunc($title, "Inscrivez le nombre total de pensées à afficher aléatoirement." & @CRLF & @CRLF & "Pour avoir un nombre illimité, inscrivez '0'.", "0")


; Presentation of "pensées"
Sleep($pausestart)
$count_loop = 0
$different_pensee1 = 0
$different_pensee2 = 0
$different_pensee3 = 0
$different_pensee4 = 0
Do
    ; Count of pensées
    $count_loop = $count_loop + 1

    ; Random selection of "pensée" but not the four last one
    Do
        $random_pensees = Random(1, $number_of_pensees, 1)
    Until ($random_pensees <> $different_pensee1 And $random_pensees <> $different_pensee2 And $random_pensees <> $different_pensee3 And $random_pensees <> $different_pensee4)
    $different_pensee4 = $different_pensee3
    $different_pensee3 = $different_pensee2
    $different_pensee2 = $different_pensee1
    $different_pensee1 = $random_pensees
    $linetext = FileReadLine($filehandle, $random_pensees)

    ; Show the "pensée" in a dialog box
    $show_pensee = MsgBox(262145, $title, "Pensée #" & $random_pensees & @CRLF & @CRLF & $linetext & @CRLF & @CRLF & "Il est " & @HOUR & ":" & @MIN & ".", $readingpause)
    If $show_pensee = 2 Then
        MsgBox(0, "Petite pensée du moment", "Arrêt du programme.", 10)
        Exit
    EndIf

;   ; Old way to show the "Pensée" - That way don't show all the line if it's to long
;       SplashTextOn($title, "Pensée #" & $random_pensees & @CRLF &$linetext & @CRLF & "Il est " & @Hour & ":" & @MIN & "." , "400", "200", "-1", "-1", 33, "Times New Roman", "20", "700")
;           Sleep($readingpause * 1000)
;       SplashOff()

    ; Pause beteween "pensées"
    Sleep($pause_between_pensees)
Until $count_loop = $end_of_loop

FileClose($filehandle)
Exit


; Function

Func getnumfunc($1, $2, $3) ; Conversion function of number
    Local $getnum
    Do
        ; Get the numnber wiht the possibility of cancelling
        ; $getnum = InputBox($1, $2, "0", "", 350, 150, 350, 350, 15) ; With a delay
        $getnum = InputBox($1, $2, $3)
        If @error = 1 Then
            MsgBox(0, $1, "Vous avez annulé la demande," & @CRLF & "le programme est arrêté.")
            Exit
        ElseIf @error = 2 Then
            MsgBox(0, $1, "Réponse trop lente," & @CRLF & "le programme est arrêté.")
            Exit
        EndIf

;       ; Old way of conversion because the first time I used 'Number()' it didn't work at all
;       Local $testnum, $icount_num, $nummax
;       $nummax = 5000
;       $icount_num = 0
;       Do
;           $testnum = "" & $icount_num
;           If $testnum = $getnum Then
;               $getnum = $icount_num
;               ExitLoop
;           EndIf
;           $icount_num = $icount_num + 1
;       Until $icount_num = $nummax
;
;       ; Verification of existence of number and if not to big
;       $verification = IsNumber($getnum)
;       If $verification = 0 Then
;           MsgBox(0, $1, "Votre chiffre dépasse " & $nummax & " ou qu'il n'a pas été bien inscrit." & @CRLF & @CRLF & "Veuillez recommencez.")
;       EndIf

        ; Conversion and verification
        $getnum = Number($getnum)
        $verification = IsNumber($getnum)
        If $verification = 0 Then
            MsgBox(0, $1, "Votre chiffre n'est pas bon." & @CRLF & @CRLF & "Veuillez recommencez.")
        EndIf

    Until $verification = 1
    Return $getnum
EndFunc   ;==>getnumfunc

Mad Dog (Maurice) Vachon, a great Canadian professional wrestler!

Share this post


Link to post
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
Sign in to follow this  
Followers 0