Jump to content

randomization


Recommended Posts

ok then after that, how do i make it execute a function? I tried execute but it was fail xD

While 1
    $1 = Random( 1, 999, 1 )
    Sleep( 100 )
    Send( "{space}" )
    Send($1)
    if $1=666 then
        MsgBox(1, "AHHH DEVIL", "DEVIL AHHHHHHHHHHHHH" )
        Execute( devilish )
    EndIf
WEnd

Func devilish()
While 1
    Sleep( 100 )
WEnd
EndFunc
Link to comment
Share on other sites

Something like:

$MIN=1
$MAX=40
$string="|"

While 1
    Sleep(10)
    Do
        $temp=Random($MIN,$MAX,1)
    Until Not StringInStr($string,"|"&$temp&"|")
    Send($temp)
    $string&=$temp&"|"
    $array=StringRegExp($string,"\|",3)
    If Ubound($array)>=($MAX-$MIN+2) Then ExitLoop
WEnd

Could also be done with arrays...

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Hi, monoceres!

What if I have many text strings, and I want to randomly pick up one of them, and do not reapeat any previous one, but after they are all finished, start over, randomizing strings, and so on ? :)

Regards,

Kiti

Link to comment
Share on other sites

I thought of this the other day, it's basically a shuffle algorithm, like what you would use to place every card in a deck in a random position, without repeating the cards.

Easiest solution I could think of, was to make 2 arrays, both the size of the data. Fill 1 with your data to be shuffled.

Then, select one at random (using AutoIt's Random(1, 52, 1) for a standard card deck), add that card to the other array at position 1.

After the card is selected, remove it from the first array, and update a variable pointing at the current position in the 2nd array.

Next run, the Random function would run, but for one less (Random(1, 51, 1)), and so on, until you only had one card, and then you'd have it shuffled.

Only problem with that solution, would be the deleting elements from an array, that causes things to slow down a lot. For those willing to try, you could actually use just 1 array, and when you pick a card to be moved, just exchange it with either ending variable (just swap values), and change your Random function to match (either update the 1 to 2 if you add things at the beginning, or 52 to 51 if you add things to the end). That gives you a nice speed boost, reduces the memory your program will eat, and is just a cool solution to the problem in general.

Have fun getting it to work.

Link to comment
Share on other sites

Like this (doing an array solution this time)

Dim $strings[5]=["hi","hello","hej","hallo","ola"]
$strings=_RandomizeArray($strings)

Local $count=0
While 1
    Sleep(100)
    Send($strings[$count])
    $count+=1
    If $count=Ubound($strings)-1 Then
        $strings=_RandomizeArray($strings)
        $count=0
    EndIf
WEnd


Func _RandomizeArray($array)
    Local $out[Ubound($array)]
    For $element In $array
        Do
            $i=Random(0,Ubound($out)-1,1)
        Until $out[$i]=""
        $out[$i]=$element
    Next
    Return $out
EndFunc

This also works for numbers of course.

:)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Well, monoceres, that will work, but it can take a lot of time, if your Random keeps picking locations that already have data in them, which can happen if you get down to the last few elements in the input array. Also, if the value in the input array was "", and is picked again, your going to overwrite it (but, that would just leave some other location at "", so that's not too bad).

That's the main reason I did mine with a changing random function, to avoid times where it might run fast, and then the next time take a lot longer to finish.

Link to comment
Share on other sites

I believe this is faster:

; More levels = better shuffling but slower execution time.
Func _RandomizeArray(ByRef $array, $level=3)
    Local $size=Ubound($array)
    
    For $crap=1 To $level
        For $i=0 To $size-1
            $rand=Random(0,$size-1,1)
            $temp=$array[$i]
            $array[$i]=$array[$rand]
            $array[$rand]=$temp
        Next
    Next
EndFunc

But the shuffling could be quite bad considering an element could be moved back and forth to the same position it original had.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Couldn't that also happen in real life? I could shuffle a deck 100 times, and a card in it could end up right back in it's original starting location.

Yeah, but I wouldn't consider shuffling card in real life as real random, but for card shuffling algorithms, fine.

I used the swap algorithm for my solitaire game in my sig for example (The Idiot).

:)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

If you don't consider shuffling in real life as real random, what do you consider real random? Would rolling dice to put the cards in order be more random? Same thing could happen there.

Not trying to argue with you, but the general perception of randomness most people have is horribly wrong.

Link to comment
Share on other sites

If you don't consider shuffling in real life as real random, what do you consider real random? Would rolling dice to put the cards in order be more random? Same thing could happen there.

Not trying to argue with you, but the general perception of randomness most people have is horribly wrong.

Something that is not affected by humans for starters. Humans are terrible at generating random stuff (just look at my grandma when she's shuffling the cards, no random there).

A well known random generator is a geiger counter. I guess I consider it a very good random generator.

I don't think there are any true random because everything happens because something else affected it, therefore if every possible parameter is known in a case, then it can be foreseen and thus not making it true random.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Here's the version that uses the faster algorithm, it works...

Dim $strings[5]=["hi","hello","hej","hallo","ola"]
_RandomizeArray($strings)

Local $count=0
While 1
    Sleep(100)
    Send($strings[$count])
    $count+=1
    If $count=Ubound($strings)+1 Then
        _RandomizeArray($strings)
        $count=0
        exit
    EndIf
WEnd


; More levels = better shuffling but slower execution time.
Func _RandomizeArray(ByRef $array, $level=3)
    Local $size=Ubound($array)
    
    For $crap=1 To $level
        For $i=0 To $size-1
            $rand=Random(0,$size-1,1)
            $temp=$array[$i]
            $array[$i]=$array[$rand]
            $array[$rand]=$temp
        Next
    Next
EndFunc

:)

Broken link? PM me and I'll send you the file!

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...