Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Milouman33 · Jan 04, 2017 at 04:56 PM · c#random

how to create a random with probability ?

Hi,

I'm trying to create a Random with probability.

I have a list of number from 0 to 10 and I generate 100 numbers from this list. My problem is I would like to generate exactly 6 times the number 10.

I tried Random.value but it's not accurate.

Is someone can help me?

thanks in advance,

Bye

 public List<int> number = new List<int> {1,2,3,4,5,6,7,8,9,10};

 public float timer;
 public int index;
 public int countGeneration;
 public int count_5;


 // Use this for initialization
 void Start()
 {
     timer = .01f;
     index = 0;
     countGeneration = 0;
     count_5 = 0;
 }
 
 // Update is called once per frame
 void Update ()
 {
     timer -= Time.deltaTime;

     if (timer <= 0 && countGeneration < 100)
     {
         timer = .01f;
         float rand = Random.value;
         if (rand <= .94f)
             index = Random.Range(0, 9);
         else
             index = 10;

         if (index == 10)
                 count_5++;
         
         countGeneration++;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Majesteit · Jan 04, 2017 at 06:03 PM 0
Share

If you want to generate the number 10 an exact amount of times, why don't you just random 94 times from 0 to 9, and add 6 numbers of 10 afterwards?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dpoly · Jan 04, 2017 at 11:22 PM

The only way to get some number to occur exactly N times is to do it explicitly, without using random. Since I assume you still want a random order, you need to generate first, then shuffle. The steps are:

  1. Create a List< int >.

  2. Add the number 10 six times.

  3. Add 94 random numbers in the range 0-9.

  4. Shuffle the list.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Milouman33 · Jan 05, 2017 at 09:06 AM 0
Share

It works :)

Thanks a lot !!!

avatar image dpoly · Jan 05, 2017 at 10:48 AM 0
Share

$$anonymous$$y pleasure. $$anonymous$$ake sure you get your shuffle algorithm right -- it's easy to screw up.

avatar image
0

Answer by PizzaPie · Jan 04, 2017 at 06:07 PM

int[] random10Indexs = new int[6]; int countGeneration; int index;

 void Start()
 {
     bool isFirst = true;

     for (int i = 0; i < 6; i++)
     {
         if (isFirst)
         {
             int index = Random.Range(0, 100);
             random10Indexs[i] = index;
             isFirst = false;
         }
         else
         {
         aaa:
             int index = Random.Range(0, 100);
             for (int j = 0; j < i; j++)
             {
                 if (index == random10Indexs[j])
                     goto aaa;
             }
             random10Indexs[i] = index;
         }
     }

 }

 void Update()
 {
     bool is10 = false;
     if (countGeneration < 100)
     {

         for (int i = 0; i < random10Indexs.Length; i++)
         {
             if (countGeneration == random10Indexs[i])
                 is10 = true;
         }
         if (is10)
             index = 9;
         else
             index = Random.Range(0, 8);
         countGeneration++;
     }
 }

Indexes that produced from the Update are from 0 to 9, to get the actual numbers 1-10 either you use them to refer to your List with the numbers or change the values one up to get directly what you want. Thats the easiest way to ensure you have 6 * 10s and the rest have the same probability to be produced. Hope that helps. Another way to do this would be to create a pool with all the numbers and start picking one by one and removing them from the pool. There are several ways to go around that problem and each have other positives and negatives. That one is closer to random but not the best for cpu and memory usage. Hope that helps , for clarifications ask away.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

292 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a Teleporter Pad send Player to other random Teleporter Pad? 1 Answer

Moving slowly copy of object to random position on x,z? 0 Answers

How to add force in random directions? 0 Answers

How to make questions random, and include from a folder not inside the unity editor 0 Answers

How to swap any two gameobjects in a field area? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges