Gambling wheel algorithm Learning
Alas, after gradually learning about the genetic algorithm, I found that the idea of using it in the game architecture may not be implemented. Although the genetic algorithm is a framework algorithm, however, it is a specific algorithm, and it is a limited algorithm, rather than a general framework.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using System. Diagnostics;
Namespace Roulette
{
Public partial class MainPage: UserControl
{
Public MainPage ()
{
InitializeComponent ();
Int [] test = new int [] {100, 23,150,220,300,400,450 };
Double [] probabilitys = RouletteWheel. GetProbability (test );
Random rand = new Random ();
Int [] rest = new int [probabilitys. Length];
Long start = DateTime. Now. Ticks;
Int start1 = DateTime. Now. Millisecond;
For (int I = 0; I <100000000; I ++)
{
Double te = rand. NextDouble ();
Int d = RouletteWheel. GetSelection (probabilitys, te );
Switch (d)
{
Case 0:
Rest [0] ++;
Break;
Case 1:
Rest [1] ++;
Break;
Case 2:
Rest [2] ++;
Break;
Case 3:
Rest [3] ++;
Break;
Case 4:
Rest [4] ++;
Break;
Case 5:
Rest [5] ++;
Break;
Case 6:
Rest [6] ++;
Break;
Case 7:
Rest [7] ++;
Break;
Case 8:
Rest [8] ++;
Break;
}
}
Long elapsed = DateTime. Now. Ticks-start;
Int elapsed1 = DateTime. Now. Millisecond-start1;
}
Public class RouletteWheel
{
Public static int GetSum (int [] _ fitness)
{
Int sum = 0;
Int index = 0;
Foreach (int item in _ fitness)
{
Sum + = _ fitness [index ++];
}
Return sum;
}
Public static int GetSelection (double [] _ probabilitys, double _ choise)
{
If (_ choise> 1.0 | _ choise <0.0)
{
MessageBox. Show ("Please Check Choise ");
Return-1;
}
Int index = 0;
Double cumulate = 0.0; // cumulative probability
While (index <_ probabilitys. Length)
{
Cumulate + = _ probabilitys [index];
If (_ choise <cumulate)
{
Return index;
}
Index ++;
}
MessageBox. Show ("Not Find This .");
Return-1;
}
Public static double GetSum (double [] _ fitness)
{
Double sum = 0;
Int index = 0;
Foreach (int item in _ fitness)
{
Sum + = _ fitness [index ++];
}
Return sum;
}
Public static double [] GetProbability (int [] _ fitness)
{
Int sum = GetSum (_ fitness );
Double [] result = new double [_ fitness. Length];
For (int I = 0; I <_ fitness. Length; I ++)
{
Result [I] = _ fitness [I]/(double) sum;
}
Return result;
}
}
}
}