質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

9658閲覧

決まった数字をランダム、または確率指定して出力したいです。

giftVS

総合スコア19

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2018/12/29 16:56

前提・実現したいこと

オブジェクトに回転を加え右回転と左回転をランダムに出現させるために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ページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答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
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Bongo

2018/12/29 22:14

回答を書いているうちに先に回答がついてしまいました...すみません。
giftVS

2018/12/30 13:21

回答ありがとうございます!最初のコードで無事にやりたいことを実現できました。 それとせっかく二つの方法を提示していただいたのでただコピペするのではなくしっかりと理解して自分の 知識にします。本当にありがとうございました。
guest

0

0から10未満の乱数を生成し、5以上かそうでないかで50%の確率になります
同様に2未満かそうでないかで判定すれば20%の確率となりますね

投稿2018/12/29 21:48

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

giftVS

2018/12/30 13:24

無事に解決しました。回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問