Rati_Ge
Rati_Ge

Reputation: 1270

Can not convert ActionScript3 to c#

Hallo I am trying to implement sorting function in c# (I wan to sort the array)

card_codes.sort(function() { return 2 * Math.floor(Math.random()) ? -1 : 1 } );

This is how ActionScript sorts array of integers in script and every time after first sort it starts with 301

but When I try to sort same array in c# it always starts with 101

here is how I sort it in c#

Array.Sort(PlayngCardCodes, (f1, f2) => 0.CompareTo(2 * (int)Math.Floor(rng.NextDouble()) == 0 ? -1 : 1));

can anyone tell me how to exactly rewrite actionscript sort to get the same result?

Upvotes: 0

Views: 301

Answers (2)

The_asMan
The_asMan

Reputation: 6403

The ActionScript code is broken

Math.random()// will result in a number from 0 to 1 like so
//0.40654489677399397

When you floor any number it will round it down. for example

0.40654489677399397 floored-->0

in otherwords

trace(Math.floor(0.40654489677399397) )// output is 0

So to sum it up

trace( Math.floor( Math.random() ) )// output is always 0


trace( 2 * Math.floor(Math.random()) ? -1 : 1 ) // output is always 1

I do not know c# but I would think you need to fix your source before you try to convert it.
However, since the ActionScript output is always 1 just make you c# function always return a 1 and be done with it :)
[EDIT]
Well I ran a test and the output is odd. Although, its not the original array probably due to the type of sorting that is being done in AS3

var card_codes:Array = new Array(2,467,8,342,37,7,6789,34,234,2)
card_codes.sort(function() { return 2 * Math.floor(Math.random()) ? -1 : 1 } );
trace(card_codes)// output is always 7,2,6789,8,34,37,234,2,342,467

Upvotes: 1

Related Questions