Jump to content

need help in my coding


Recommended Posts

Hi.

I have 1 to 20. I need to take these numbers and rearrange them in random order. How do I do the code without getting duplicates? Meaning if 5 is selected, it should skip 5. I tried create a code to do this but somehow it just got trapped in its own loop at the last few nos.

Here is my code. If anyone can do a simpler and better than mine I would appricate it. Else can someone help me untangle the loop? :)

My Code:

==============

AutoItSetOption ("TrayIconDebug", 1)

$cycle_no=20

$cycle=0

$numleft=$cycle_no

$file=""

$ran_num=0

while $numleft<>0

$ran_num=Random(1, $cycle_no, 1)+1

do

if StringInStr($file, $ran_num&"!")<>0 then

msgbox(0,"ran_num",$ran_num)

$ran_num=$ran_num+1

if $ran_num>20 then $ran_num=1

endif

until StringInStr($file, $ran_num&"!")=0

$file=$file&$ran_num&"!"&@CR

$numleft=$numleft-1

msgbox(0,$numleft,$file)

WEnd

Link to comment
Share on other sites

This worked well for me.

#include <Array.au3>
Dim $result[20]

$count = 0

While $count <> 20
    ConsoleWrite ($count & @CRLF)
    $random = Random (1, 20, 1)
    $search = _ArraySearch ($result, $random)
        If @error = 6 Then
            $result[$count] = $random
            $count +=1
        EndIf
WEnd

_ArrayDisplay ($result)
_ArraySort ($result, 1); Sorts the array so that you can see if there are duplicates
_ArrayDisplay ($result); displays the sorted array

Hope that helps :)

Link to comment
Share on other sites

I already answered this but I don't know what happened to the post.

I'm just winging it here so be shure to checl the help file

Dim $Array [1]
Do
$Ran_Num = Round(Random(1,20),1)
If _ArraySearch[$Array, $Ran_Num,1) = -1 Then
   _ArrayAdd($Array, $Ran_Num)
EndIf
Until Ubound($Array) -1 = 20

I may have left something out but it will give you the idea. I use something similar for a couple of Lottery games I wrote but I don't have that code available right now.

Edit:

You could remove that Dim statement and use

$Array = StringSplit("","")

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

This fills an array with the numbers 1 to 20. Then it steps through the list 3 times and swaps values around randomly. This ends up with a mixed up list

Global $MaxRandom = 20
Global $Randoms[$MaxRandom]
For $i = 0 to $MaxRandom - 1
    $Randoms[$i] = $i + 1     ; Define the numbers
Next
For $j = 1 to 3     ; Modify stop number to adjust "mixiness"
    For $i = 0 to $MaxRandom - 1     ; Now mix them up
        $RandomTmp1 = $Randoms[$i]
        $i2 = Random(0, $MaxRandom - 1, 1)
        $Randoms[$i] = $Randoms[$i2]
        $Randoms[$i2] = $RandomTmp1
    Next
Next
ConsoleWrite(@LF)
For $i = 0 to $MaxRandom - 1
    ConsoleWrite($Randoms[$i] & @LF)
Next
ConsoleWrite(@LF)
Edited by bluebearr
BlueBearrOddly enough, this is what I do for fun.
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...