您当前的位置: 首页 >  游戏

Clank的游戏栈

暂无认证

  • 12浏览

    0关注

    186博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

unity游戏开发中的随机算法

Clank的游戏栈 发布时间:2022-09-06 09:31:29 ,浏览量:12

随机相关内容 C#实现 Unity直接可用

对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习unity的零基础小白,也有一些正在从事unity开发的技术大佬,欢迎你来交流学习。

洗牌代码

//Fisher-Yates shuffle

static void Shuffle(T[] array)

{

int n = array.Length;

for (int i = 0; i < n; i++)

{

int r = i + Random.Range(0, n - i);

T t = array[r];

array[r] = array[i];

array[i] = t;

}

}

带权重随机代码

static public int GetRandomWeightedIndex(float[] weights)

{

// Get the total sum of all the weights.

float weightSum = 0;

for (int i = 0; i < weights.Length; ++i)

{

weightSum += weights[i];

}

// Step through all the possibilities, one by one, checking to see if each one is selected.

int index = 0;

int lastIndex = weights.Length - 1;

while (index < lastIndex)

{

// Do a probability check with a likelihood of weights[index] / weightSum.

if (Random.Range(0, weightSum) < weights[index])

{

return index;

}

// Remove the last item from the sum of total untested weights and try again.

weightSum -= weights[index++];

}

// No other item was selected, so return very last index.

return index;

}

伪随机C系数生成代码

static public float CfromP(float p)

{

float Cupper = p;

float Clower = 0f;

float Cmid;

float p1;

float p2 = 1f;

while (true)

{

Cmid = (Cupper + Clower) / 2f;

p1 = PfromC(Cmid);

if (Mathf.Abs(p1 - p2) p)

{

Cupper = Cmid;

}

else

{

Clower = Cmid;

}

p2 = p1;

}

return Cmid;

}

private float PfromC(float C)

{

float pProcOnN = 0f;

float pProcByN = 0f;

float sumNpProcOnN = 0f;

int maxFails = Mathf.CeilToInt(1f / C);

for (int N = 1; N

关注
打赏
1661738913
查看更多评论
立即登录/注册

微信扫码登录

0.1745s