Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by dropthepress · Jul 09, 2020 at 01:09 AM · randomrandom.rangerandomspawningrandomizingrng

Simple random spawning system for 10 items without duplicates

Here is a simplified version of what I need:


  1. I have 10 items and I want to spawn 1 item at random every 30 seconds.

  2. I do not want any repeats of active items.

  3. So if 1 item spawned at 30 seconds, then I want a random item from the remaining 9 items to spawn at 1 minute.

  4. If all items are active, then nothing spawns.

  5. If an item is destroyed, then I want it to be added back to the spawnable list.


I have tried using a Random.Range(0, 10) to generate 10 numbers, but I want to remove certain numbers from the list the next time it runs.


So maybe I need a way to generate a Random.Range(0, 10), but somehow remove active numbers from the list?


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tuinal · Jul 09, 2020 at 01:34 AM

Two options; check if you've spawned it already, or pre-generate 10 shuffled numbers.

For the first option, make a List and add what you've spawned, then check if the list .Contains what you're about to spawn, and if so re-roll the dice. This isn't great though as it's a (very much theoretically) infinite loop if the RNG gives you an invalid number infinity times.

So, better to just make an array of int[], populate it with 1-10, then shuffle it in Start(). Draw the next number from the array each time you spawn by incrementing a pointer.

   void reshuffle(int[] vals)
         {
             // Knuth shuffle algorithm :: courtesy of Wikipedia :: and the other Answers post I ripped it from :)
             for (int t = 0; t < vals.Length; t++ )
             {
                 int tmp = vals[t];
                 int r = Random.Range(t, vals.Length);
                 vals[t] = vals[r];
                 vals[r] = tmp;
             }
         }

  
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
avatar image
0

Answer by Eno-Khaon · Jul 09, 2020 at 03:04 AM

Another way you can approach this is by setting up a list with each option, removing them from the list as they're activated, then returning it to the list after use.

 public int valueCount = 10;
 List<int> exampleValues;

 void Start()
 {
     exampleValues = new List<int>();
     for(int i = 1; i <= valueCount; i++)
     {
         exampleValues.Add(i);
     }
 }

 public int GetRandomValue()
 {
     // This randomly grabs from the remaining quantity of possible values
     int randomElement = Random.Range(0, exampleValues.Count);
     int returnValue = exampleValues[randomElement];
     exampleValues.Remove(randomElement);
     return returnValue;
 }

 public void ReturnValue(int valueToReturn)
 {
     exampleValues.Add(valueToReturn);
 }
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

138 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

Related Questions

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

how can i random a spawning but not same gameobject 1 Answer

Randomising list of integers and removing them from list once the task assigned to the integer in the project is completed 0 Answers

Random.range multiple instantiations without repetition 1 Answer

random range can stop when the index matches the input and without duplicate question display 2 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