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
1
Question by J830930 · Dec 27, 2014 at 08:03 AM · c#arrayrandom

How to make array to non-same random value?

using UnityEngine; using System.Collections;

public class Apple : MonoBehaviour {

 public int[] a;     
 

 void Start () {

     a = new int[4];

     Shuffle ();


 }

 public void Shuffle(){

     for (int i = 0; i < a.Length; i++) {
             a [i] = Random.Range (0, 20);
                 Debug.Log (a [i]);
         }

 }


Result > 0,9,18,9 .. '9' is Same. i dont want that value. How to make array to non-same random value?

}

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
Best Answer

Answer by EmmaEwert · Dec 27, 2014 at 01:23 PM

Generate a list of integers between your desired min and max values.

Extract the amount of elements you need, updating the list.

Back-of-envelope suggestion:

 List<int> ints = new List<int>();
 List<int> values = new List<int>();
 int min = 0;
 int max = 20;
 int needed = 5;
 
 for (int i = min; i <= max; ++i) {
   ints.Add(i);
 }
 
 for (int i = 0; i < needed; ++i) {
   int index = Random.Range(0, ints.Count);
   values.Add(ints[index]);
   ints.RemoveAt(index);
 }

Note that this solution does no bounds checking, and will throw an exception when you request more values than exist between max and min.

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 J830930 · Dec 27, 2014 at 04:23 PM 0
Share

I LOVE YOU!! im find good way!!! Oh my God! very very Thanks!!!!! I think I've found the gold Two friends ver ver very! thank!! so much!!

avatar image EmmaEwert · Dec 27, 2014 at 08:00 PM 0
Share

It should be noted that the answer by Pkninja16 is asymptotically slower; O(∞), in fact (as the Random#Range call is not guaranteed to ever return an acceptable candidate and as such cannot guarantee anything about the required repetitions, or even whether the while loop will ter$$anonymous$$ate at all).

The solution I have proposed is not the most performant, but for sufficiently small ranges of possible values, it should be performant enough. It has a time complexity of about O(n+m), where n = max - $$anonymous$$ and m = needed - depending on the implementation of List#Add, List#[] and List#RemoteAt, of course.

For the approach above to be repeatedly used in practice, you probably ought to think about what should happen when you ask for more elements than there are possible values. I have edited the original answer to clarify the lack of bounds checking.

avatar image
0

Answer by Pkninja16 · Dec 27, 2014 at 10:04 AM

You can check if the number that you generated is already in the array by using the IndexOf(int) method.

 for (int i = 0; i < a.Length; i++) {
      int random = Random.Range(0,20);
      while(a.IndexOf(random)> -1)
      {
           random = Random.Range(0,20);//This will keep on generating a number until there is no repeat
      }
      a [i] = random;
      Debug.Log (a [i]);
 
 }

Now be weary that if the array is longer, the values from 0-20 may be all taken up so the while loop will keep running forever because there is no way for it to make a number that isn't already in the array. Hope this helps!

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 J830930 · Dec 27, 2014 at 12:31 PM 0
Share

VeryVeryVery Thank! but .. error message .. i'm Crying T_T ..

error CS1501: No overload for method IndexOf' takes 1' arguments

IndexOf method is died. why? Please one more answer.

avatar image fafase · Dec 27, 2014 at 04:39 PM 0
Share

No good answer, you have a while loop, which means your game is about to stop until the right value is found. If you need 10 values out of 20 possible ones, as you go on, you are more and more likely to hit a miss, by the time you look for the 10th value, you have 50% chances to never find an unused value. Wrong.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Pick between two floats 2 Answers

C# array equal to another array minus one entry. 1 Answer

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Accessing Instance Bool from other script 2 Answers

Multiple Cars not working 1 Answer


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