Snippet GetRandomEven()

dare2care

New Member
Reaction score
3
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 < 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
 

DrEvil

FCRI Associate!
Reaction score
111
GetRandomInt(startNum/2,endNum/2) * 2

EDIT : Yes , your example is correct.
( My bad , didn't see :confused: )

EDIT2 : Arg , thought u said How not S'how's

This is not really a snippet... Anyone could do this.
But let's see what other people think...
 

SerraAvenger

Cuz I can
Reaction score
234
not fairly accurate.
JASS:
function GetRandomEven takes integer min, integer max returns integer result
       return GetRandomInt((min+1) / 2, (max+1) / 2) * 2
endfunction


function GetRandomOdd takes integer min, integer max returns integer result
       return GetRandomInt((min) / 2, (max) / 2) * 2 + 1
endfunction


EDIT: Still not totally ok^^
You will have to take care when min = max and min being odd/even (depending on the function), in which case there should be no result at all; It will give out the next highest integer though.
 

dare2care

New Member
Reaction score
3
@
JASS:
function GetRandomEven takes integer min, integer max returns integer result
       return GetRandomInt((min+1) / 2, (max+1) / 2) * 2
endfunction


JASS:
call GetRandomEven(6, 10)


That gives me a random even int between 7 and 11? Why do you need that?

Is there any round function in JASS?
 

Romek

Super Moderator
Reaction score
963
I fail to see how this is even remotely useful.
And in any case where it is useful, I doubt people would come to this site, and search out a "EvenRandomInteger" snippet.
 

SerraAvenger

Cuz I can
Reaction score
234
Any function to round up?

Add +(dividend/2) before dividing?

EDIT:

JASS:
function GetRandomEven takes integer min, integer max returns integer // no result here!
       if (min != max) then // forgot a then!
             return GetRandomInt((min+1) / 2, (max+1) / 2) * 2
       endif
       return 0 // min is most probably odd, in which case returning min is a bad idea. 
endfunction


please learn JASS syntax before you post snippets^^
 

Romek

Super Moderator
Reaction score
963
Add 0.99999 to the result.
Or add 0.5 to round correctly.
 

SerraAvenger

Cuz I can
Reaction score
234
Add 0.99999 to the result.
Or add 0.5 to round correctly.

In an integer division?
lol. My dear romek, you totally missed the point^^

EDIT: Yeah. Now he made the syntax even worse. GJ, romek...
@dare2care: adding 0.5 will convert your beloved integer to a real. Your GetRandomInt() function, however, requires integers as arguments, not reals. Now you will even need two additional function calls (R2I()) which will make the code even harder to read and slower.

And even if you did, nothing had changed becouse your friggin integer division first rounds down, then you add 0.5, and round down again. You, too, did totally miss the point >_>

EDIT2:
Please fix the syntax before making it even worse. You still return integer "result" and have no then after your if.
 

Romek

Super Moderator
Reaction score
963
This doesn't even compile.
"function GetRandomEven takes integer min, integer max returns integer result"
would syntax.

And what if min is odd? Then you have a problem.
I ask again, why would anyone want this?
It's quite useless. And complicated for something so useless.

JASS:
return R2I(GetRandomReal(min, max)/2.)*2


> In an integer division?
One would assume you're converting.
 

Romek

Super Moderator
Reaction score
963
> Fixed errors. It now compiles properly.
No it doesn't.
 

SerraAvenger

Cuz I can
Reaction score
234
This doesn't even compile.
"function GetRandomEven takes integer min, integer max returns integer result"
would syntax.

And what if min is odd? Then you have a problem.
Am I speaking french or why does noone read my posts >_>
I said the same in my last three posts^^

JASS:
return R2I(GetRandomReal(min, max)/2.)*2

Another approach that does neither take into account the
GetRandomEven( 3, 3 ) nor the ( odd, odd )/( odd, even )/( even, odd ) problem

> In an integer division?
One would assume you're converting.

Just for rounding up^^?
 

dare2care

New Member
Reaction score
3
> Fixed errors. It now compiles properly.
No it doesn't.

It does with JassHelper.

>Another approach that does neither take into account the GetRandomEven( 3, 3 )

Already fixed that, look at original post.
 

SerraAvenger

Cuz I can
Reaction score
234
JASS:
integer result

should not compile.
should be
JASS:

instead

Nevertheless there is still an uneeded local variable == 2 unnecessary lines and 4 bytes of unneeded memory.
And it still doesn't work.
Just ffs copy and paste
a)
Code:
return R2I(GetRandomReal(min, max)/2. + 0.9)*2
or
b)
JASS:
             return GetRandomInt((min+1) / 2, (max+1) / 2) * 2


>Another approach that does neither take into account the GetRandomEven( 3, 3 )

Already fixed that, look at original post.

I quoted romek's approach.
 

Romek

Super Moderator
Reaction score
963
> It does with JassHelper.
That's a bug with Jasshelper then.

Anyway, this is graveyarded.
It's utterly useless, (should have) syntax errors, and is poorly coded (especially for something so simple).
I'm surprised this even managed to get as many replies as it has.

> GetRandomEven( 3, 3 ) nor the ( odd, odd )/( odd, even )/( even, odd ) problem
Well, what would you expect GetRandomEven(3, 3) to return? ._.
 
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