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 /
  • Help Room /
avatar image
0
Question by ilovemypixels · Oct 22, 2012 at 03:00 PM · randomnumbergenerate

Generate a random number different to the last

Hi there,

I need to generate a random number in unity that will be different to the last generated random number.

I have the following code and this is how you would do it in something like flash.

 function getUniqueRandomSectionNumber(){
     var tempSectionNum : int = Random.Range(0, totalSections);
 
     if(tempSectionNum == lastCreatedSectionNum){
         getUniqueRandomSectionNumber();
     } else {
         lastCreatedSectionName = tempSectionNum;
         return tempSectionNum;
 
     }
 }

However in Unity I don't seem to be able to run the function from inside itself. In order to try again on the random number.

Hope this makes sense and thanks for any help

Will

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 Fattie · Oct 27, 2012 at 12:09 PM 0
Share

just add a random number between one and the number. modulo by the size. it's that simple.

4 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Random username · Oct 22, 2012 at 03:12 PM

You could have the code in a while loop that only ends when the last random number is not equal to the current one. e.g:

 function getUniqueRandomSectionNumber(){
     var hasFoundUniqueRandomSelection : bool;
     var tempSectionNum : int;
 
     while (!hasFoundUniqueRandomSelection) {
        tempSectionNum  = Random.Range(0, totalSections);
        if(tempSectionNum != lastCreatedSectionNum){
            hasFoundUniqueRandomSelection = true;
            lastCreatedSectionName = tempSectionNum;
        }
     }
     return tempSectionNum;
 }

Apologies if I've made any mistakes, not accustomed to working with javascript.

Comment
Add comment · Show 3 · 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 ilovemypixels · Oct 22, 2012 at 03:18 PM 0
Share

Thanks for that I figured out that my code above does actually work too I just needed to declare the type as int.

function getUniqueRandomSectionNumber() : int { }

avatar image Owen-Reynolds · Oct 22, 2012 at 03:50 PM 0
Share

The OPs code is using recursion, which is the same thing as that loop.

Real error is needing to add return to the true part of the if: `RETURN getUniqueSecNum();`.

You'd think "but the else has a return." But say you roll the last number twice in a row. You've got a chain of three functions. the last one rolls a fresh num and returns to the previous function, which called it. That one also needs to return the number up the line. And if that doesn't make sense, just use the loop :-)

avatar image Fattie · Oct 22, 2012 at 05:09 PM 1
Share

you would never, ever do it that way

But there is an unbelievably easier way to write that!

 result = random
 while (  result != previous ) result = random

that's all it is.

Goodness! You young people, really :-)

avatar image
-1

Answer by srikaran_p · Sep 10, 2016 at 12:17 PM

Sir/Ma'am, even I had the same problem before but I fixed it with an if-condition.

 currentNumber = Random.Range (0, markers.Length);
             if (lastNumber != currentNumber) 
             {
                 lastNumber = currentNumber;
             } else 
             {
                 currentNumber = Random.Range (0, markers.Length);
                 lastNumber = currentNumber;
             }
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 Fattie · Sep 10, 2016 at 12:23 PM 0
Share

this does not work, because you could AGAIN draw the same random number :)

the correct answer is in my short comment under the question. it is a very famous algorithm in computer science - very simple.

cheers

avatar image srikaran_p · Sep 11, 2016 at 09:41 AM 0
Share

It worked really well for me after testing the same function for about more than 100 times. And, there was only 7 numbers in my array so it could have been repeated but it didn't, so that's why I posted this answer. Well, I'm a very young programmer and I looked for an answer and I also looked at your comment but it wasn't what I wanted and then , I coded this myself. It worked really well so I posted this answer even though it is 4 years old. But anyways, I can't say wrong to an experienced programmer.

avatar image
0

Answer by TinoCle · Nov 13, 2016 at 10:38 PM

 int rand = Random.Range(0,totalSections);
 while(rand==RandyAnterior){
         rand = Random.Range(0,totalSections);
 }
 RandyAnterior = rand;
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 ayhaab-pasha · Oct 24, 2018 at 07:23 AM

I know its 2018 but people would still be stumbling upon this question. So, maybe this would be helpful for someone. I was trying to spawn enemy planes in Augmented Reality. I wanted them to be randomly spawned but at a distance. For example, if one plane is spawned at 0,0,0. I wanted the next plane to be spawned at 0+20 or 0-20 on the X-Axis. Like, the new plane should spawn at 20 or more points from the present spawned point in positive and negative direction. Going through the forum i came across a code and modified it and then came to this:

  void RandonDistanceAxis()
  {
      distanceX = Random.Range (-70, 70);
      while (distanceX <= tempx + 20 && distanceX >= tempx - 20 )
      {
          distanceX = Random.Range (-70, 70);
      }
      tempx = distanceX;
      distanceY = Random.Range (-10,10);
      distanceZ = Random.Range (60,90);
  }

While declaring the tempx variable, give it a value of something out of the Range. I used 71. And it worked perfectly fine. Hope this helps. Cheers.

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

13 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

Related Questions

Random float but not the same 2 Answers

Can I generate values outside of the Random.insideUnitCircle?? 1 Answer

randomly shoot using number generator 0 Answers

How to randomize a stages with no repetition? 1 Answer

How to check with Random Range If Exits (Float) 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