Jump to content

[SOLVED] randomly change an array


FMS
 Share

Recommended Posts

Hello,

I'm trying to randomly change some cells in a array on a given percentage.

at this point I 've a array whit all 0's and want to change some cells to 1.

I'm not shure how to do this in a good coding sort of way.

Also maybe there is a build in function whish I'm not aware of.

Does somebody know how I can do this in a easy way?

I was trying to get the total count of cells and get the percentage of it.

And was stuck when i wanna change the cell.

please advice, thanks in advanced for your help.

#include <Array.au3>
Global $percentage = 0.2
Global $aArray[Random(10,30,1)][Random(10,30,1)]

   For $x = 0 To UBound($aArray,1) - 1
      For $y = 0 To UBound($aArray,2) - 1
         $aArray[$x][$y] = 0
      Next
   Next

_ArrayDisplay($aArray)

randomize()

_ArrayDisplay($aArray)

Func randomize()
   Local $total_to_change = ((UBound($aArray,1) * UBound($aArray,2)) / 100) * $percentage
   ConsoleWrite( "$total_to_change = " & $total_to_change & @LF )
   ConsoleWrite( "total in array = " & (UBound($aArray,1) * UBound($aArray,2)) & @LF )
EndFunc

 

Edited by FMS

as finishing touch god created the dutch

Link to comment
Share on other sites

  • Moderators

FMS,

Use Random to set the array elements to 1 or 0 as you create the array:

#include <Array.au3>

Global $percentage = 0.2
Global $aArray[Random(1, 10, 1)][Random(1, 10, 1)]

_ArrayDisplay($aArray)

For $x = 0 To UBound($aArray, 1) - 1
    For $y = 0 To UBound($aArray, 2) - 1
        $aArray[$x][$y] = ((Random(0, 1) < $percentage) ? (1) : (0))
    Next
Next

_ArrayDisplay($aArray)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...