TjW

Random Variable But Must Use All

12 posts in this topic

For $i = 0 To 3

^I am using to go through and select each one, I would like it to randomly choose a variable of 0, 1, 2, 3 but not stop until all 4 and tried as $i

This just goes in order 0, 1, 2, 3

Dim $test[4]
 
for $i = 0 to 3
$test[$i] = "Hello i am " & $i
Next
$test[0] = UBound($test)
$r = Random(1,$test[0])
$m=$test[$r]
msgbox (0,"this is $m:",$m & " The random was: " & $r)
 
This does choose random but don't know how to make it choose randomly and eliminate number "x" as I go.
 
This can be done completely different if need be as well, any help appreciated

Share this post


Link to post
Share on other sites



TjW,

Here's one way...

#include <array.au3>

local $aValues = ['water', 'earth', 'fire', 'ice'], $idx

while ubound($aValues)

    $idx = random(0, ubound($aValues) - 1,1)
    ConsoleWrite($aValues[$idx] & @CRLF)
    _arraydelete($aValues,$idx)

wend

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

Share this post


Link to post
Share on other sites

Welcome to AutoIt and the forum!

Could you please do yourself and us a favour and enclose your script in AutoIt code tags? That's the blue "A" icon in the editor.

This formats and syntax highlights the code and greatly enhances readability (as you cen see in post #2 by kylomas) :)


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

Welcome to AutoIt and the forum!

Could you please do yourself and us a favour and enclose your script in AutoIt code tags? That's the blue "A" icon in the editor.

This formats and syntax highlights the code and greatly enhances readability (as you cen see in post #2 by kylomas) :)

Not a problem

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I believe you're looking for something like this:

#include <array.au3>

Dim $test[4]
dim $lastnumber[4]

for $i = 0 to 3
$test[$i] = "Hello i am " & $i
Next

dim $max = UBound($test)-1

 for $i = 0 to 3
$r = StringReplace(Round(Random(-1,$max)), "-1", "0")
while _ArraySearch($lastnumber, $r) <> -1
   $r = StringReplace(Round(Random(-1,$max)), "-1", "0")
WEnd
$m=$test[$r]
$lastnumber[$i] = $r
msgbox (0,"this is $m: "&$r, " The random was: " & $m)
Next

_ArrayDisplay($lastnumber)
Note: This has a non terminating while loop in it that will keep guessing random numbers until it guesses one that hasn't been guessed, in certain instances this can take a VERY long time, but it's only "frozen" once out of the 10 times I tested it, so I don't know if that is going to be a problem in all application or if the thread just froze on me. Edited by nullschritt

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I wrote this as a test of the functionality and it works flawlessly every time

#include <array.au3>
$timer = TimerInit()
$time = 0

for $i2=1 to 100

Dim $test[4]
dim $lastnumber[4]

for $i = 0 to 3
$test[$i] = "Hello i am " & $i&" |"
Next

dim $max = UBound($test)-1

 for $i = 0 to 3
$r = StringReplace(Round(Random(-1,$max)), "-1", "0")
while _ArraySearch($lastnumber, $r) <> -1
   $r = StringReplace(Round(Random(-1,$max)), "-1", "0")
WEnd
$m=$test[$r]
$lastnumber[$i] = $r
ConsoleWrite($m)
Next
$Time += timerdiff($timer)/1000
ConsoleWrite(@CRLF)
ConsoleWrite("Order "& _arraytostring($lastnumber, ",")&@CRLF)
$timer = TimerInit()
Next

ConsoleWrite("Time Average: "&$time/100&" Seconds"&@CRLF)
Note: I'm using -1 as 0 because for some reason random seems to almost NEVER pick 0.

Edit: Updated code to measure speed. Seems to take about 1/720th of a second to guess the 4 random numbers with no repeats. But you can use as many values as you want, I tested it with 200 and it took 0.110866811824646 Seconds or about 1/10th of a second on average.

Edited by nullschritt

Share this post


Link to post
Share on other sites

_ArrayShuffle is your best friend here: exactly 1 line of code.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If you want to avoid (why not ?) to include Array.au3, you can just do something like this :

Local $aNumbers[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


For $i = 0 To UBound($aNumbers) - 1
    $iRand = ( $i = UBound($aNumbers) - 1 ? UBound($aNumbers) - 1 : Random($i, UBound($aNumbers) - 1, 1) )
    $iPrev = $aNumbers[$iRand]
    $aNumbers[$iRand] = $aNumbers[$i]
    $aNumbers[$i] = $iPrev
    
    ConsoleWrite($aNumbers[$i] & @CRLF)
Next

_ArrayDisplay($aNumbers)

An other scatty way (i found it funny) without using an array (if you want just a few change in your existing code) :

Local $iMin = 0, $iMax = 10

For $i = $iMin To $iMax
    $iN = _RandomLoop($iMin, $iMax)
    ConsoleWrite($iN & @CRLF)
Next



Func _RandomLoop($iStart, $iEnd)
    Local Static $sShuffle, $iCount, $iStartPrev, $iEndPrev
    
    If $iStartPrev <> $iStart Or $iEndPrev <> $iEnd Then 
        $iStartPrev = $iStart
        $iEndPrev = $iEnd
        
        $iCount = $iEnd - $iStart + 1
        $sShuffle = ""

        For $i = $iStart To $iEnd ; build the String in order
            $sShuffle &= $i & ","
        Next
    
        For $i = $iCount To 0 Step -1 ; shuffle numbers in the string
            $sShuffle = StringRegExpReplace($sShuffle, "((?:-?\d+,){" & Random(0, $i, 1) & "})(-?\d+,)(.*)", "$1$3$2")
        Next
    EndIf
    
    Local $iVal = StringRegExpReplace($sShuffle, ",[-\d]*", "")
    $sShuffle = StringRegExpReplace($sShuffle, "^-?\d+,", "")
    
    Return $iVal <> "" ? Number($iVal) : SetError(1, 0, "") ; @error = 1 if all numbers have been chosen and return is blank
EndFunc

(just an unnecessary idiocy I think ...)

Edited by jguinch

Share this post


Link to post
Share on other sites

@jguinch your scripts return the same value more than once. He wants each value to only be selected once. ( : (So you need to keep track of the previous selections)

Share this post


Link to post
Share on other sites

Guess what:

#include <array.au3>

Local $aNumbers[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
_ArrayShuffle($aNumbers)
_ArrayDisplay($aNumbers)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

my no array attempt:

Local $aNumbers[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
$z = 0

While 1

$i = random(0, ubound($aNumbers) - 1 , 1)

    If $aNumbers[$i] = ""  Then
        continueloop
    Else
        consolewrite($aNumbers[$i] & @CRLF)
        $aNumbers[$i] = ""
        $z += 1

        If $z = ubound($aNumbers) Then exitloop

    EndIf

Wend

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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