Jump to content

Random words


Recommended Posts

  • Moderators

spel565,

If I understand yor question correctly, place the words into an array and then use a random index number:

Global $asWords[4] = [3, "Tom", "Dick", "Harry"]

While 1

    $iIndex = Random(1, 3, 1) ; random integer 1, 2 or 3

    MsgBox(0, "Result", $asWords[$iIndex])

WEnd

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

  • Moderators

spel565,

First, please do not send PMs asking for help - it is considered rude. If you post your questions in the forums, then everyone can learn - which is why we are here! >_<

OK, you want word 1 more often than word 2, and word 2 more often than word 3, etc.

Two ways to do this:

1. As you suggested, add more elements to the array - but you need to increase the size of the array - look carefully at the difference between your code and mine:

; Your code 
Global $asWords[4] = [3, "1", "1", "1", "2", "3", "3", "2", "5"]
$iIndex = Random(1, 8, 1)
MsgBox(0, "Result", $asWords[$iIndex])

; My code
Global $asWords[9] = [3, "1", "1", "1", "2", "3", "3", "2", "5"]
$iIndex = Random(1, 8, 1)
MsgBox(0, "Result", $asWords[$iIndex])

Spotted the difference? if not, look carefully at the Global $asWords[#] part!

But you could also use another method which uses fewer resources:

Global $asWords[4] = [3, "Tom", "Dick", "Harry"]

While 1

    $nRandom = Random()     ;produce a random number between 0 and 1

    Switch $nRandom
        Case 0 To .5        ; this will be true 50% of the time
            $iIndex = 1
        Case .5 To .85      ; this will be true 35% of the time
            $iIndex = 2
        Case Else           ; this will be true only 15% of the time
            $iIndex = 3
    EndSwitch

    MsgBox(0, "Result", $asWords[$iIndex])

WEnd

To change the ratios, just alter the numbers in the Case statements. Your array remains at the smaller size, so fewer resources used.

Ask if anythng is unclear.

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

  • Recently Browsing   0 members

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