Snippet GetRandomEven()

dare2care

New Member
Reaction score
3
>integer result
>should not compile.
>should be
>Jass:
>integer

Didn't understand

___

Updated so when 2 odd equal integers are given it returns as 0.
 

SerraAvenger

Cuz I can
Reaction score
234
Code:
function ABC takes integer stuff returns [B]integer[/B]
  local integer a = stuff * 5
  return a
endfunction

Code:
function IsEven takes integer number returns boolean
    // when dividing by a real, it automatically converts the type
    return [B]( number / 2 ) *2 == number[/B]
endfunction

function GetRandomEven takes integer min, integer max returns [B]integer[/B]
    if min == max and[B] IsEven(min) [/B]then
    [B]    return min[/B]
    elseif max < min then
        return GetRandomInt( [B](max+1) / 2[/B], [B](min+1) / 2[/B] ) * 2       
    else
        return GetRandomInt( [B](min+1) / 2[/B], [B](max+1) / 2[/B] ) * 2
    endif
    [B]return 0[/B]
endfunction

I bolded everything I changed.
 

SerraAvenger

Cuz I can
Reaction score
234
My eyes say it does. Don't have Anything to check here though, so I can't tell.

EDIT: Plz also read my last post.
 

dare2care

New Member
Reaction score
3
My eyes say it does. Don't have Anything to check here though, so I can't tell.

EDIT: Plz also read my last post.

Already did and updated.

EDIT: If I put in 1,1 it outputs 2.

EDIT2: Fixed

JASS:
function IsEven takes integer number returns boolean
    return ( number / 2 ) * 2 == number
endfunction

function GetRandomEven takes integer min, integer max returns integer
    if min == max and IsEven(min) then
        return min
    elseif max &lt; min then
        return GetRandomInt((max + 1) / 2, (min + 1) / 2 ) * 2       
    elseif (min != max) then // this
        return GetRandomInt((min + 1) / 2, (max + 1) / 2 ) * 2
    endif
    return 0
endfunction
 

SerraAvenger

Cuz I can
Reaction score
234
The line was correct, but the fix doesn't seem. shouldn't it be
JASS:
    elseif (min &lt; max) then // this line


instead?

EDIT: Missread. I read something overly complicated :D
 

JerseyFoo

1/g = g-1
Reaction score
40
Shows you how to get a random even integer.

JASS:
function IsEven takes integer number returns boolean
    return ( number / 2 ) * 2 == number
endfunction

function GetRandomEven takes integer min, integer max returns integer
    if min == max and IsEven(min) then
        return min
    elseif max &lt; min then
        return GetRandomInt((max + 1) / 2, (min + 1) / 2 ) * 2       
    elseif min != max then
        return GetRandomInt((min + 1) / 2, (max + 1) / 2 ) * 2
    endif
    return 0
endfunction


Examples
JASS:
GetRandomEven(1, 1) //outputs 0
GetRandomEven(9, 10) // ouputs 10
GetRandomEven(6, 8) // outputs 6 or 8
GetRandomEven(8, 6) // ouputs 6 or 8

JASS:
function GetRandomEven takes integer min, integer max returns integer
    set min = GetRandomInt( min, max )
    return ModuloInteger( min + ModuloInteger( min, 2 ), max + ModuloInteger( max, 2 ) )
endfunction


Let illegal operations be illegal?
 
Reaction score
333
Mother of all random range functions:

JASS:
function GetRandomModRange takes integer lowBound, integer highBound, integer divisor, integer lowR, integer highR returns integer
    local integer d = highBound - lowBound
    
    if (d &lt; highR - lowR) then
        set highR = lowR + d
        set lowR = lowBound - (lowBound / divisor) * divisor
        if (lowR &lt; 0) then
            set lowR = lowR + divisor
        endif
    endif
    
    set lowR = GetRandomInt(lowR, highR)
    set lowBound = (lowBound + (divisor-1)) - lowR
    set highBound = highBound - lowR
    
    set d = lowBound / divisor
    if (d &lt; 0 and d * divisor != lowBound) then
        set lowBound = d - 1
    else
        set lowBound = d
    endif
    
    set d = highBound / divisor
    if (d &lt; 0 and d * divisor != highBound) then
        set highBound = d - 1
    else
        set highBound = d
    endif

    return GetRandomInt(lowBound, highBound) * divisor + lowR
endfunction


JASS:
function GetRandomEven takes integer lowBound, integer highBound returns integer
    return GetRandomModRange(lowBound, highBound, 2, 0, 0)
endfunction

function GetRandomOdd takes integer lowBound, integer highBound returns integer
    return GetRandomModRange(lowBound, highBound, 2, 1, 1)
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top