Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

weighted random c#

public static readonly int RATIO_CHANCE_A = 10;
public static readonly int RATIO_CHANCE_B = 30;
//                         ...
public static readonly int RATIO_CHANCE_N = 60;

public static readonly int RATIO_TOTAL = RATIO_CHANCE_A
                                       + RATIO_CHANCE_B
                                         // ...
                                       + RATIO_CHANCE_N;

Random random = new Random();
int x = random.Next(0, RATIO_TOTAL);

if ((x -= RATIO_CHANCE_A) < 0) // Test for A
{ 
     do_something1();
} 
else if ((x -= RATIO_CHANCE_B) < 0) // Test for B
{ 
     do_something2();
}
// ... etc
else // No need for final if statement
{ 
     do_somethingN();
}
Comment

weighted random c#


public static readonly int RATIO_CHANCE_A = 10;
public static readonly int RATIO_CHANCE_B = 30;
//                         ...
public static readonly int RATIO_CHANCE_N = 60;

public static readonly int RATIO_TOTAL = RATIO_CHANCE_A
                                       + RATIO_CHANCE_B
                                         // ...
                                       + RATIO_CHANCE_N;

Random random = new Random();
int x = random.Next(0, RATIO_TOTAL);

if ((x -= RATIO_CHANCE_A) < 0) // Test for A
{ 
     do_something1();
} 
else if ((x -= RATIO_CHANCE_B) < 0) // Test for B
{ 
     do_something2();
}
// ... etc
else // No need for final if statement
{ 
     do_somethingN();
}

Comment

PREVIOUS NEXT
Code Example
Csharp :: unity hide mesh 
Csharp :: c# make first letter uppercase 
Csharp :: shuffle arraylist c# 
Csharp :: c# datetime remove time 
Csharp :: how to change the color of a sprite in unity 
Csharp :: unique items in list c# 
Csharp :: unity normalize float 
Csharp :: c# sqlite query 
Csharp :: c# get all the column names from datagridview 
Csharp :: C# removing the last value of an array 
Csharp :: change sprite of a sprite unity 
Csharp :: c# foreach dictionary 
Csharp :: convert comma separated string to array c# 
Csharp :: how to make a mouse down condition in C# 
Csharp :: c# append text to file 
Csharp :: polybius square 
Csharp :: int value from enum in C# 
Csharp :: unity camera follow player 3d smooth 
Csharp :: c# unity get name of object 
Csharp :: how to destroy a gameobject in c# 
Csharp :: google sheet script change text color 
Csharp :: unity set mouse 
Csharp :: c# string contains any of list 
Csharp :: int to bool c# 
Csharp :: c# sum of list 
Csharp :: unity get perlin noise 3d 
Csharp :: unity play sound effect 
Csharp :: c# enum syntax 
Csharp :: how to check if the value is alphabet only in c# 
Csharp :: c# oops concept 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =