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 world-is-torture · Feb 01, 2018 at 11:33 PM · random.range

Problem with random Range, returns always same number

well, as it says the title, ive made a small function to return a random vector3, but always returns the same values, not doing random. It always returns 0.1,0.1,0.1

 void RandomVector(Vector3 a)
 {
 float uno = Random.Range(0.05f, 0.15f);
 float dos = Random.Range(0.05f, 0.15f);
 float tres = Random.Range(0.05f, 0.15f);
     a = new Vector3(uno, dos, tres);
     Debug.Log(a);
 }
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

3 Replies

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

Answer by Obsessi0n · Feb 02, 2018 at 12:01 AM

The vector is correct the debug is just not showing the full float number change your debug for:

 Debug.Log(a.ToString("F4"));
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 Ginxx009 · Feb 02, 2018 at 01:56 AM

In order not to repeat a number twice you might try storing the value of the first number. For example .

 int [] array = new int[]{0,1,2,3,4,5,6};
  List<int>list = null;
  void Start(){
      list.AddRange(array);
  }
  int GetUniqueRandom(bool reloadEmptyList){
       if(list.Count == 0 ){
           if(reloadEmptyList){
               list.AddRange(array); 
           }
           else{
               return -1; // here is up to you 
          }
      }
     int rand = Random.Range(0, list.Count);
     int value = list[rand];
     list.RemoveAt(rand);
     return value;
  }

I hope it helps you.

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 world-is-torture · Feb 02, 2018 at 09:43 AM

It was a problem related to seeds and stuf, couldnt fix it, it always returned the same amount no matter what, but i found an easier way, ill put it here in case someone needs.

with only one sentence i could do all that shit.

vector3random = Random.insideUnitSphere * DistanciaMax;

Random.insideUniteSphere returns a random value inside a sphere of radius 1.

Comment
Add comment · Show 5 · 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 ♦ · Feb 02, 2018 at 09:44 AM 0
Share

You can change the seed.

avatar image Bonfire-Boy · Feb 02, 2018 at 12:13 PM 1
Share

Are you saying that Obsession's answer is incorrect? I would expect the code you posted in the question to always log the vector as (0.1, 0.1, 0.1), simply because of the way you're logging it (all random numbers between 0.05 and 0.15 are 0.1 when displayed to 1 decimal place). It will not be always creating the same Vector3 though (unless you were to re-seed with the same number before each call).

avatar image Bunny83 Bonfire-Boy · Feb 02, 2018 at 12:33 PM 1
Share

Right, also the original code uses always a positive number for each component, so it will always be in the positive octant so only covering "1/8"th of the whole range. "insideUnitSphere" will return an vector in any direction.

avatar image Bonfire-Boy Bunny83 · Feb 02, 2018 at 12:35 PM 0
Share

Good point, plus the ranges in the OP start at a non-zero number, so it's a shell in that positive octant.

avatar image Bunny83 · Feb 02, 2018 at 12:39 PM 1
Share

It's great that insideUnitSphere does what you want but it doesn't answer your original question and does something very different from what you've done in your original code. For the future if you have a problem you should describe the problem you want to solve. You seem to have choosen a solution and where stuck there. From your question it wasn't clear if you actually wanted a vector in the (+,+,+) octant or a generally random vector. Also your method "RandomVector" looks really strange as you pass in a vector as parameter which you replace inside the method. However since Vector3 is a value type the value you passed in won't be changed outside the method.

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

78 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

Related Questions

Random.Range not working? 2 Answers

Generate Random Numbers a Distance Apart From Each Other 1 Answer

Random.Range method "left-hand side" error 2 Answers

Random Range Without Duplicates 2 Answers

Loading random scenes, but never the same 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