Jump to content

Recommended Posts

Posted

Hiya,

I thought this might be handy to share. Some chance calculations.

#Region _Chance
$Random = Random(1, 100, 1)
$Chance = _Chance($Random)

If $Chance = 1 Then
    MsgBox(0, "SUCCES", "A " & $Random & "% chance was succesfull!")
ElseIf $Chance = 0 Then
    MsgBox(0, "FAIL", "A " & $Random & "% chance failed!")
Else
    MsgBox(0, "ERROR", "Ehhhhhhh?")
EndIf
#EndRegion

#Region _ChanceOutOf
$Random1 = Random(1, 5, 1)
$Random2 = Random(6, 10, 1)
$Chance = _ChanceOutOf($Random1, $Random2)

If $Chance = 1 Then
    MsgBox(0, "SUCCES", "A " & $Random1 & " out of " & $Random2 & " chance was succesfull!")
ElseIf $Chance = 0 Then
    MsgBox(0, "FAILED", "A " & $Random1 & " out of " & $Random2 & " chance was failed!")
Else
    MsgBox(0, "ERROR", "Ehhhhhhh?")
EndIf

; #FUNCTION # =======================================================
; Name............; _Chance()
; Description.....; Calculates the chance in percent
; Syntax..........; _Chance($iPercent)
; Parameters......; int: $iPercent - Chance rate
; Return Values...; 0 | Succes
;                   1 | $iPercent isn't an integer
; Author..........; AlmarM (Almar Mulder)
; Modified........; -
; Remarks.........; -
; Related.........; _ChanceOutOf()
; Link............; -
; Example.........; Yes
; ===================================================================
Func _Chance($iPercent)
    If Not IsInt($iPercent) Then Return SetError(1, 0, 0)
    
    $iChance = Round(Random(1, 100, 1), 0)
    
    If $iChance <= $iPercent Then
        SetError(0, 0, 0)
        
        Return 1
    EndIf
EndFunc     ; ==> _Chance

; #FUNCTION # =======================================================
; Name............; _ChanceOutOf()
; Description.....; Calculates the chance x out of y
; Syntax..........; _ChanceOutOf($iChance, $iOutOf)
; Parameters......; int: $iChance - Chance rate
;                   int: $iOutOf - 'out of' number
; Return Values...; 0 | Succes
;                   1 | $iChance isn't an integer
;                   2 | $iOutOf isn't an integer
; Author..........; AlmarM (Almar Mulder)
; Modified........; -
; Remarks.........; -
; Related.........; _Chance()
; Link............; -
; Example.........; Yes
; ===================================================================
Func _ChanceOutOf($iChance, $iOutOf)
    If Not IsInt($iChance) Then Return SetError(1, 0, 0)
    If Not IsInt($iOutOf) Then Return SetError(2, 0, 0)
    
    $iFormule = Round((100 / $iOutOf) * $iChance, 0)
    $iRandom = Round(Random(1, 100, 1), 0)
    
    If $iRandom <= $iFormule Then
        SetError(0, 0, 0)
        
        Return 1
    EndIf
EndFunc     ; ==> _ChanceOutOf

AlmarM :D

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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
×
×
  • Create New...