前提・実現したいこと
オブジェクトに回転を加え右回転と左回転をランダムに出現させるためにrotspeedに
1.2と-1.2をそれぞれ50%の確率で出力したいです。
また1.2を80%-1.2を20%で確率を指定して出力もしたいのですがこれもわかりません。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class NegiController : MonoBehaviour 6{ 7 8 float fallSpeed; 9 float rotSpeed; 10 11 void Start() 12 { 13 this.fallSpeed = 0.03f; 14 this.rotSpeed = 1.2f; 15 } 16 17 18}
試したこと
サイコロと同じ仕組みと思いいろいろ調べたのですが0~10といった数字の範囲のみを指定したものしか見つけられず。
リストという仕組みを見つけたのですが導入の仕方がわかりませんでした。そもそもリストで正しいのかもわかりません。
初歩的な質問だと思いますが、知恵を貸していただきたいです。
それと大変恐縮ではありますがサンプルコードなどあればとても助かります。
よろしくお願いします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
ご質問者さんの状況でしたら、一案として、まず0.0~1.0の一様な乱数を発生させ、その乱数から偏らせたい割合を引くというのはどうでしょう。たとえば0.8を引くと、80%の確率でマイナスの、20%の確率でプラスの値になるはずです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class NegiController : MonoBehaviour 6{ 7 public float Ratio = 0.8f; 8 9 float fallSpeed; 10 float rotSpeed; 11 12 void Start() 13 { 14 this.fallSpeed = 0.03f; 15 this.rotSpeed = (Random.value - this.Ratio) < 0.0f ? 1.2f : -1.2f; 16 17 // 得られた値を計算に組み込む必要がなければ、こっちの方がシンプルですね 18 // this.rotSpeed = Random.value < this.Ratio ? 1.2f : -1.2f; 19 } 20}
あるいは、重み付けの抽選を行うアルゴリズム – Lancarse Blogのような案はいかがでしょうか?
今回は二者択一ですが、いくつも候補がある場合はそういったやり方が考えられるでしょう。
下記のような抽選装置を作って実験してみたところ...
C#
1using System.Collections.Generic; 2using System.Linq; 3using UnityEngine; 4 5public class LotteryPool<T> 6{ 7 private readonly Dictionary<T, float> pool = new Dictionary<T, float>(); 8 private float weightSum; 9 10 public void Add(T item, float weight) 11 { 12 this.pool[item] = weight; 13 this.UpdateWeightSum(); 14 } 15 16 public bool Remove(T item) 17 { 18 var result = this.pool.Remove(item); 19 this.UpdateWeightSum(); 20 return result; 21 } 22 23 public void Clear() 24 { 25 this.pool.Clear(); 26 this.UpdateWeightSum(); 27 } 28 29 public T Draw() 30 { 31 var integratedWeight = 0.0f; 32 var random = Random.value * this.weightSum; 33 var result = this.pool.Keys.FirstOrDefault(); 34 foreach (var pair in this.pool) 35 { 36 var item = pair.Key; 37 var weight = pair.Value; 38 integratedWeight += weight; 39 if (random <= integratedWeight) 40 { 41 result = item; 42 break; 43 } 44 } 45 46 return result; 47 } 48 49 public void PrintWeights() 50 { 51 Debug.Log("Pool has:"); 52 foreach (var pair in this.pool) 53 { 54 Debug.Log($"\t{pair.Key}: {pair.Value}"); 55 } 56 57 Debug.Log($"Weight sum: {this.weightSum}"); 58 } 59 60 private void UpdateWeightSum() 61 { 62 this.weightSum = this.pool.Values.Sum(); 63 } 64}
C#
1using System.Collections; 2using System.Collections.Generic; 3using System.Linq; 4using UnityEngine; 5 6public class BallSpawner : MonoBehaviour 7{ 8 public int Count; 9 public float Interval; 10 public BallWeightPair[] Balls; 11 12 private IEnumerator Start() 13 { 14 var wait = new WaitForSeconds(this.Interval); 15 var pool = new LotteryPool<GameObject>(); 16 foreach (var ball in this.Balls) 17 { 18 pool.Add(ball.Ball, ball.Weight); 19 } 20 21 pool.PrintWeights(); 22 var results = new List<GameObject>(); 23 for (var i = 0; i < this.Count; i++) 24 { 25 results.Add(Instantiate(pool.Draw(), this.transform.position + Random.insideUnitSphere * 0.25f, Quaternion.identity)); 26 yield return wait; 27 } 28 29 var ballCount = results.Count; 30 var groupedCount = results.GroupBy(o => o.name); 31 var goldCount = groupedCount.First(g => g.Key.Contains("Gold")).Count(); 32 var silverCount = groupedCount.First(g => g.Key.Contains("Silver")).Count(); 33 var bronzeCount = groupedCount.First(g => g.Key.Contains("Bronze")).Count(); 34 var ironCount = groupedCount.First(g => g.Key.Contains("Iron")).Count(); 35 Debug.Log($"Number of balls: {ballCount}"); 36 Debug.Log($"Gold: {goldCount}({(goldCount * 100.0) / ballCount:F1}%)"); 37 Debug.Log($"Silver: {silverCount}({(silverCount * 100.0) / ballCount:F1}%)"); 38 Debug.Log($"Bronze: {bronzeCount}({(bronzeCount * 100.0) / ballCount:F1}%)"); 39 Debug.Log($"Iron: {ironCount}({(ironCount * 100.0) / ballCount:F1}%)"); 40 } 41 42 [System.Serializable] 43 public struct BallWeightPair 44 { 45 public GameObject Ball; 46 public float Weight; 47 } 48}
下図のように金の玉10%、銀の玉20%、銅の玉30%、鉄の玉40%の割合で玉を発生させることができました。
投稿2018/12/29 22:13
編集2018/12/30 10:53総合スコア10807
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/29 22:14
2018/12/30 13:21