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 /
avatar image
0
Question by Mystirb · May 11, 2016 at 06:16 AM · randomgenerationnumberpreventlast

Random number different from previous generated.

Hi! I'm really all new to this programming stuff. I'm having a problem with Unity's random number generator as it has a tendency (well at least in my case) to consecutively repeat the same number a lot.

I've tried figuring how to code it myself to no avail. When I use a while loop the engine tends to crash which makes me believe I've written it wrong.

I just want a c# script that prevents it from repeating the same number that was generated previously.

I've tried making this script but all I keep getting is 0. Help please?

void randomController () { a = Random.Range (0, 4);

     if (a == b)
         a = Random.Range (0, 4);
     else
         a = b;

                 
 }
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 Bonfire-Boy · May 11, 2016 at 11:29 AM 0
Share

You always want to generate a random number, right? So it shouldn't be inside an if clause like in your version. You want to generate a random number first, then test if it's the same as the previous one (and keep regenerating till it isn't) - this is what Griffo's answer shows.

The other thing about your code is that you don't show us what b is. It must be referred to elsewhere in your code. Without that it's hard to tell what your code is even trying to do.

6 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Griffo · May 11, 2016 at 09:45 AM

I use ..

 private int a;
 private int oldNumber;
 
 void randomController()
 {
      a = Random.Range (0, 4);
      oldNumber = a;
      if(a == oldNumber)
      {
           randomController();
      }
 }
Comment
Add comment · Show 4 · 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 Bonfire-Boy · May 11, 2016 at 11:23 AM 0
Share

You're assigning ins$$anonymous$$d of comparing on line 8

avatar image Griffo Bonfire-Boy · May 11, 2016 at 11:34 AM 1
Share

Corrected ..

avatar image Dave-Carlile · May 11, 2016 at 12:40 PM 2
Share

Probably better off using a loop. Recursion seems awfully expensive for what you're trying to do. And in the (admittedly extremely unlikely) chance you get the same number often enough you could overflow the stack. Just doesn't seem like a good idea to leave that up to chance.

avatar image Bonfire-Boy · Oct 24, 2018 at 05:04 PM 0
Share

I've just noticed that this isn't going to work as written. The test will always succeed because you set oldnumber to be equal to a immediately before testing if it is equal to a. Easy enough to fix, but I agree with Dave, it's simpler and better with a loop...

 int previous = a;
 while (a == previous)
 {
     a = Random.Range(0,4);
 }
avatar image
1

Answer by tanoshimi · May 11, 2016 at 06:22 AM

Random.value will do that just fine. http://docs.unity3d.com/ScriptReference/Random-value.html

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
1

Answer by YoungDeveloper · May 11, 2016 at 09:49 AM

Don't expect equally filled pattern with Random, because that's what it is, it's random. Argument i'm getting 0 a lot is not really valid, because you know it can be whatever it can be in those scopes.

What exactly you mean a lot?

 void Start(){
     for(int i = 0; i < 1000; i++){
         Debug.Log(Random.Range(0,4).ToString());
     }
 }

If youll get thousand zeros in debug ill eat my hat.

Comment
Add comment · Show 1 · 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 meat5000 ♦ · May 11, 2016 at 12:00 PM 0
Share

https://en.wikipedia.org/wiki/Infinite_monkey_theorem

:P

avatar image
1

Answer by meat5000 · May 11, 2016 at 12:02 PM

If you are truly getting repeat results when you re-run the same tests with 'Random', change the seed.

http://docs.unity3d.com/ScriptReference/Random-seed.html

Also int overload of Random.Range is Maximally exclusive. In actual english this means that the upper number is never reached. 4 will never occur.

Also, the idea of removing the object/number when it is selected is known as a Shuffle Bag.

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
1

Answer by kaphuochung24h · Mar 24, 2017 at 10:53 PM

Maybe this is the old answer for the topic owner, but i hope it can help someone who can't get the right answer.

 for (int i = 0; i < pos.Length; i++)
         {
             pos[i] = Random.Range(1, 7);
             if (i > 0)
             {
                 previous = 0;
                 check = 0;
                 do
                 {
                     if(previous >= i){
                         previous=0;
                         check = 0;
                     }
                     if (pos[i] == pos[previous])
                     {
                         pos[i] = Random.Range(1, 7);
                     }
                     else
                     {
                         check++;
                     }
                     previous++;
 
                 } while(check == i);
             }
         }

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
  • 1
  • 2
  • ›

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

10 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

Related Questions

random number generator problem 1 Answer

Randomly Generated Levels 3 Answers

How do I split a rectangle into random smaller rectangles? 1 Answer

Having trouble with PlayerPrefs in my script. 1 Answer

How to create a perlin noise heightmap with the values being the highest around a point? 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