onClipEvent

I need to call a different random number several times

22 posts in this topic

Hi, AutoIt forums!

I recently discovered Autoit, and I'm loving it! But, since I'm not really that good of a coder I thought I might get some help here.

The thing I need help with is:

I need to get several random numbers. They all need to be different for my program to work optimally.

This is what I'm trying to do:

 
Local $rand = Random (110, 160, 1)
 
ConsoleWrite($rand & @CRLF)
ConsoleWrite($rand & @CRLF)
ConsoleWrite($rand & @CRLF)
ConsoleWrite($rand & @CRLF)
 

 

But, as you probably know, this outputs the same number four times.

fFD1rbn.png

My question is, how can I make it so that it prints four different randomly generated numbers? I know that I could do something like "$rand_1 = ... $rand_2 = ...." etc, but there has to be a better way?

Thanks for any help! :)

Share this post


Link to post
Share on other sites



Something like this. A new number is created when you call function Random.

ConsoleWrite(Random (110, 160, 1) & @CRLF)
ConsoleWrite(Random (110, 160, 1) & @CRLF)
ConsoleWrite(Random (110, 160, 1) & @CRLF)
ConsoleWrite(Random (110, 160, 1) & @CRLF)

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

 

Something like this. A new number is created when you call function Random.

ConsoleWrite(Random (110, 160, 1) & @CRLF)
ConsoleWrite(Random (110, 160, 1) & @CRLF)
ConsoleWrite(Random (110, 160, 1) & @CRLF)
ConsoleWrite(Random (110, 160, 1) & @CRLF)

 

Yes, this would work, but I need to be able to change the random variable easily. If I need to print out 20 random numbers, it would be cumbersome to change every one of the lines.

 

only have 1 consolewrite

$var1 = Random(1,15,1)

ConsoleWrite($var1 & @CRLF)

 

You're not helping here.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

#include <Array.au3>

Local $HowMany = 10;how many random numbers you need
Local $randTmp
Local $array[$HowMany]
For $x = 1 To $HowMany
    Do
        $rand = Random(110, 160, 1)
    Until Not StringInStr($randTmp, "#" & $rand & "#")
    $randTmp &= "#" & $rand & "#"
    $array[$x - 1] = $rand
    ConsoleWrite($array[$x - 1] & @CRLF)
Next
_ArrayDisplay($array)
Edit: btw pls dont do small random gaps with this code, Like:

random betwean 100 and 110 and that you need 9 or more random numbers (more that 10 is imposible ofc), note that something like that will lead to lag trying to finde last random number or numbers.

if you dont need unique random number remove do until loop

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

You're not helping here.

 

I am helping, you should be more clear on what you need from the very beginning.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

 
#include <Array.au3>

Global $gaRandom = _RandomToArray(10, 120, 140, 1, Default, True)
_ArrayDisplay($gaRandom)

Func _RandomToArray($iCount, $iMin, $iMax, $iFlag = 0, $iSeed = @SEC, $bShuffle = False)

    If $iCount < 1 Then Return SetError(1, 0, 0)
    $iFlag = ((IsKeyword($iFlag) Or $iFlag < 1) ? 0 : 1)
    $iSeed = ((IsKeyword($iSeed) Or $iSeed < 0) ? @SEC : $iSeed)
    $bShuffle = (($bShuffle = Default) ? False : $bShuffle)

    SRandom($iSeed)

    Local $aRet[$iCount]
    For $i = 0 To $iCount - 1
        $aRet[$i] = Random($iMin, $iMax, $iFlag)
    Next

    ; if you want to shuffle the array for a bit more randomness
    Local $nTmp, $nRndm
    If $bShuffle Then
        For $i = 0 To $iCount - 1
            $nTmp = $aRet[$i]
            $nRndm = Random(0, $iCount - 1, 1)
            $aRet[$i] = $aRet[$nRndm]
            $aRet[$nRndm] = $nTmp
        Next
    EndIf

    Return $aRet
EndFunc 

The first parameter, $iCount is how many random numbers you want to return.  The last parameter, $bShuffle offers one more attempt at randomness with shuffling the array indices, $iMin, $iMax, $iFlag are the same as Random() function.

Edit:

Forgot $iSeed, see SRandom in help file.

Edit2:

Decided to change Seed method... and poof, I'm done

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Thanks for the help everyone!

I asked a friend of mine, who codes in Python how he would solve this problem in Python. He gave me some mock-up code, and I ended up with this.

Is this a proper way to do it, or should I stick with what you guys wrote for me above?

Local $number = 4
Local $generate
func rand($generate)
    $generate = Random(110, 160, 1)
    return $generate
EndFunc
 
for $i = 1 To $number
    ConsoleWrite(rand($generate) & @CRLF)
Next
Edited by onClipEvent

Share this post


Link to post
Share on other sites

Your code does nothing that 1 line of Random(110, 160, 1) wouldn't do.

This:

Local $number = 4
Local $generate
func rand($generate)
    $generate = Random(110, 160, 1)
    return $generate
EndFunc
 
for $i = 1 To $number
    ConsoleWrite(rand($generate) & @CRLF)
Next

Is the same as:

Local $number = 4

for $i = 1 To $number
    ConsoleWrite(Random(110, 160, 1) & @CRLF)
Next

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

 

Your code does nothing that 1 line of Random(110, 160, 1) wouldn't do.

This:

Local $number = 4
Local $generate
func rand($generate)
    $generate = Random(110, 160, 1)
    return $generate
EndFunc
 
for $i = 1 To $number
    ConsoleWrite(rand($generate) & @CRLF)
Next

Is the same as:

Local $number = 4

for $i = 1 To $number
    ConsoleWrite(Random(110, 160, 1) & @CRLF)
Next

 

That is completely true, however, I will need the random function later on. And every time I need it, it has to have the same values as everywhere else. Therefore I thought it'd be useful to create a function for it.

Edited by onClipEvent

Share this post


Link to post
Share on other sites

Well, I provided a complete solution to what you were asking.  Guess I wasted my time really... but you're not passing anything to the function so:

Local $number = 4
func _rand110To160()
    Return Random(110, 160, 1)
EndFunc
 
for $i = 1 To $number
    ConsoleWrite(_rand110To160() & @CRLF)
Next

I guess that would do it for you, your function is descriptive, and no need to pass a param.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

This is a slightly different method using _ArrayShufle(). Numbers will always be unique.

;

#include <Array.au3>

Global $gaRandom = _RandomToArray(10, 120, 140)

_ArrayDisplay($gaRandom)

; Access the array elements.
For $i = 0 to UBound($gaRandom) -1
    ConsoleWrite($gaRandom[$i] & @CRLF)
Next

Func _RandomToArray($iCount, $iMin, $iMax)

    If $iCount < 1 Or $iMin > $iMax Then Return SetError(1, 0, 0)
    Local $iBound = $iMax - $iMin + 1
    If $iBound < $iCount Then Return SetError(2, 0, 0)

    Local $aArray[$iBound]
    For $i = 0 to $iBound - 1
        $aArray[$i] = $iMin + $i
    Next
    _ArrayShuffle($aArray)
    ReDim $aArray[$iCount]

    Return $aArray
EndFunc

Share this post


Link to post
Share on other sites

I don't understand some of the code posted, and forgive me if this seems like I'm trying to hijack this thread, but I do know that some of the posted code doesn't give a random number different than previously generated random numbers.  Suppose you were coding a random number generator to pick lottery tickets.  Each number generated must not be the same as a previously generated number.  There must be some double check to make sure the newly generated number doesn't match a previously generated number.  I could code this, but it would be bulky with lots of If $newNum = $oldNum1 OR $newNUM = $oldNum2 OR ...etc.  Is there an elegant and efficient way to perform this kind of check?

_aleph_

Oh, czardas, you just answered my question as I was writing it.

Kudos!


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

you just answered my question as I was writing it.

Kudos!

 

That happens to me all the time lol. :)

Share this post


Link to post
Share on other sites

Neat, I didn't realize _ArrayShuffle made it into the help file... I've been using what is from my code above for 8 years now with the same name.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thank you for the help everyone, I really learned a lot today!

Share this post


Link to post
Share on other sites

Well, I provided a complete solution to what you were asking.  Guess I wasted my time really... but you're not passing anything to the function so:

Local $number = 4
func _rand110To160()
    Return Random(110, 160, 1)
EndFunc
 
for $i = 1 To $number
    ConsoleWrite(_rand110To160() & @CRLF)
Next

I guess that would do it for you, your function is descriptive, and no need to pass a param.

 

The reason I'm passing the variable is so that it'll be easier to extend the script later on. I plan on putting my for-thingy in a function.

Share this post


Link to post
Share on other sites

I like it, czardas.  It works wonderfully.  It does get confused when I ask for 10 different integers from 1 to 9, though.  Hell, I get confused with that.

Nevertheless, your code joins my arsenal, with your credits incorporated, of course.

Thanks,

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

Neat, I didn't realize _ArrayShuffle made it into the help file... I've been using what is from my code above for 8 years now with the same name.

 

I don't think you was around when Melba23 added this function.

Share this post


Link to post
Share on other sites

I like it, czardas.  It works wonderfully.  It does get confused when I ask for 10 different integers from 1 to 9,

 

You confused me too. Use error checking @error to see if the function failed.

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