Jump to content

Script that gives one random number.


Recommended Posts

Hello,

Here is the idea. I got the idea to make a script that would randomly give me a number (or better, a name) between 1 and 32 (each number for a name). But this script wouldn't give for example the number 1 once it has already been given!

For example : 1 = Pipeline.

The script randomly gives "Pipeline".

I launch the script a second time and it gives "Pipeline" again! No!! I don't want that :-)

Once one number/name has been chosen, it can't be given one more time UNTIL the 31 other numbers/names have been given after.

Question :

-What commands should I look for? (I mean on the AutoIt HelpFile)

I already looked for the command "Random" but it didn't help me a lot :-s

Thanks!

Edited by K3vin
Link to comment
Share on other sites

Create a random number between 1 and 32. random(1,32,1). Then write your random number to the registry.

Next time you start. Create a random number. Check if number is already in registry. If so, create a new numer, until you find the one that's not in the registry. Look for random(), RegRead() and RegWrite()

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Create a random number between 1 and 32. random(1,32,1). Then write your random number to the registry.

Next time you start. Create a random number. Check if number is already in registry. If so, create a new numer, until you find the one that's not in the registry. Look for random(), RegRead() and RegWrite()

Cheers

Kurt

<{POST_SNAPBACK}>

This sounds like a good idea, but as the list gets longer your number-picking routine gets exponentially slower.

A better way is this:

Make an array of numbers, sequentially. Now scramble the array (like shuffling cards) and, like /dev/null said, write it to the registry.

Now each time you need a number, pop one off that array. If there is none left, generate a new list and start over.

This method insures that you go through all numbers before getting another one again.

Link to comment
Share on other sites

This sounds like a good idea, but as the list gets longer your number-picking routine gets exponentially slower.

yes, you're right. However, I think with 32 numbers it won't take too long. Anyway, a better solution would be:

1. read all previous numbers from registry

2. create an array $missing, which contains numbers not yet created

3. create random number from 1..$missing[0]

4. pick $missing[$random]

5. write $random to registry

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

yes, you're right. However, I think with 32 numbers it won't take too long. Anyway, a better solution would be:

1. read all previous numbers from registry

2. create an array $missing, which contains numbers not yet created

3. create random number from 1..$missing[0]

4. pick $missing[$random]

5. write $random to registry

Cheers

Kurt

<{POST_SNAPBACK}>

IMO that seems needlessly complicated. Try this out:

CODE

Func _ArrayShuffle(ByRef $Array)

Dim $i, $j, $k

If IsArray($Array) Then

For $i = 1 to $Array[0]

$j = Random(1,$Array[0])

$k = $Array[$i]

$Array[$i] = $Array[$j]

$Array[$j] = $k

Next

EndIf

EndFunc

Func _RandomUnique($Highest)

Dim $Return

$Highest = Int($Highest)

Dim $aNums = StringSplit(regread('HKLM\Software\AutoIt v3\AutoIt\Custom','Random' & $Highest),',')

$Return = _ArrayPop($aNums)

If $aNums[0] <= 1 Then ;need to create a new list

Dim $aNums[$Highest + 1]

$aNums[0] = $Highest

For $i = 1 to $Highest

$aNums[$i] = $i

Next

_ArrayShuffle($aNums)

If $Return = '' Then $Return = _ArrayPop($aNums)

$aNums[0] = UBound($aNums) - 1

EndIf

RegWrite('HKLM\Software\AutoIt v3\AutoIt\Custom','Random' & $Highest, 'REG_SZ', _ArrayToString($aNums, ',', 1))

Return $Return

EndFunc

The function _RandomUnique(x) will return a number 1..x (inclusive) that will not repeat a number until all other numbers have been picked.
Link to comment
Share on other sites

Well, i was working on this.... for a file

works the first time but... arrays arent my thing (yet)

#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>
; I always include alot of stuff - then i dont have the problem later

Dim $DLine; [32]

; place this file anywhere you want
If Not FileExists(@HomeDrive & "\Temp\list.txt") Then
    $list = "bill,george,john,mat,deb,frank,missy,hank,fred,shawana,bob,terry,etc"
    FileWrite(@HomeDrive & "\Temp\list.txt", $list)
EndIf

$NFile = FileOpen(@HomeDrive & "\Temp\list.txt", 0)
    
; Check if file opened for reading OK
    If $NFile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
    EndIf
    
; Read in line(s) of text until the EOF is reached
    While 1
        $NLine = FileReadLine($NFile)
        If @error = -1 Then ExitLoop
        If $NLine <> "" Then
            $DLine = StringSplit( $NLine,",")
        EndIf
    WEnd
    FileClose($NFile)

FileDelete(@HomeDrive & "\Temp\list.txt") 

    
_ArrayDisplay( $DLine, "UnSorted" );>>>>>>>>> for testing


_ArraySort( $DLine)

    
$Name_to_use = _ArrayPop ($DLine); removes the last entry ???


_ArrayDisplay( $DLine, "Sorted" );>>>>>>>>> for testing

$list=$Dline

If $DLine[0] >= 2 then FileWrite(@HomeDrive & "\Temp\list.txt", $List )
    
    
MsgBox(0,"Test","Name to use is " & $Name_to_use)

i think outside the box(registry)

maybe one of these guys who really know thier stuff can see the direction here and fix it

8)

NEWHeader1.png

Link to comment
Share on other sites

IMO that seems needlessly complicated.  Try this out:

Complicated? I don't think so...

#include <Array.au3>

func _getUniqueRandom($min,$max)
    dim $numbers[$max+1]
    local $nUnused, $regval

    for $i = $min to $max
    $regval = RegRead('HKLM\Software\AutoIt\Randoms',"Random" & $i) 
    if $regval <> 1 then 
       $nUnused = $nUnused + 1
       $numbers[$nUnused] = $i
    endif
    next

    if $nUnused = 0 then return -1

    $random = Random(1,$nUnused,1)

    RegWrite('HKLM\Software\AutoIt\Randoms',"Random" & $numbers[$random],"REG_DWORD",1)

    return $numbers[$random]
endfunc

msgbox(0,"","Random: " & _getUniqueRandom(1,32))

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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
 Share

  • Recently Browsing   0 members

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