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 acmeydan · Oct 11, 2015 at 08:15 PM · arrayfloatrandom.rangerandomization

How to put Random.range in float array?

Hi I'm not sure am I correctly ask, sorry if i make mistake.

I have a script which have an array of float values. This script is holding data about when i kill an enemy. It adds value of pointsToEarn to my general score. I want to make it randomize. I dont want to give "pointsToEarn" a static value, I want to give it a random value, such as random.range(1,5). But I dont understand how should i put Random.Range inside it.

Can you help me? Thank you for your patience.

 public float[] pointsToEarn = new float[2];
 
 (...)
 
 Anotherscript.anotherFunction(0, pointsToEarn[0]);


Comment
Add comment · Show 3
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 Denvery · Oct 11, 2015 at 08:25 PM 0
Share

Hello!

I need to clarify: do you want to make random array's length in new float[2]? Or you want to extract value by random index in Anotherscript.anotherFunction(0, pointsToEarn[0]);?

avatar image acmeydan Denvery · Oct 11, 2015 at 08:42 PM 0
Share

Hello! and thanks for help.

I wanto to "add" or "make" random value to pointsToEarn[0]. Something like;

 Anotherscript.anotherFunction(0, pointsToEarn[0].Random.Range(1,5));
avatar image acmeydan · Oct 11, 2015 at 09:09 PM 0
Share

ah I understand, you told me declare this before put in value... Ok got it. And it works, thanks.

3 Replies

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

Answer by Denvery · Oct 11, 2015 at 08:47 PM

something like this: pointsToEarn[0] = UnityEngine.Random.Range(-10f, 10f) ?

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 acmeydan · Oct 11, 2015 at 09:12 PM 0
Share

if you can convert it to answer, then i can vote it for best answer.

avatar image Denvery acmeydan · Oct 12, 2015 at 09:05 AM 0
Share

I'm glad that this it was useful for you

avatar image
1

Answer by willparsons · Oct 11, 2015 at 09:03 PM

From what I can tell, you're trying to add a score of 1 to 5 randomly. This is the best way to do it:

 public float maxPointsToEarn = 5;
 
 // add to score (yours may be different)
 void AddScore(float points)
 {
   score += points;
 }
 
 // somewhere else
 AddScore(Random.Range(1, maxPointsToEarn));
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 acmeydan · Oct 11, 2015 at 09:13 PM 0
Share

thank you for your help. Your method is also useful, but

 pointsToEarn[0] = UnityEngine.Random.Range(-10f, 10f)

is what i need.

avatar image
1

Answer by Statement · Oct 11, 2015 at 09:39 PM

I hope this ain't too much. But I wanted to show three ways of using random numbers with arrays.

Start calls 3 functions. Each of them shows a different way to use Random.Range.

  • The first example just stores random values into an array.

  • The second example pick random values from an array.

  • The third example got random ranges in the array. To clarify, you could access knownRanges[0].Value 10 times, get 10 random values each time, but within the range of [100, 150].

RandomRangeArrayExamples.cs

 using UnityEngine;
 using StringBuilder = System.Text.StringBuilder;
 
 public class RandomRangeArrayExamples : MonoBehaviour
 {
     StringBuilder logger = new StringBuilder();
 
     void Start()
     {
         DemoRandomNumbersInArray();
         DemoKnownNumbersInArray();
         DemoKnownRangesInArray();    
         print(logger);
     }
 
     /*
         DemoRandomNumbersInArray
           number > 7.994198
           number > 0.01712418
           number > -7.934887
           number > -3.391624
     */
     void DemoRandomNumbersInArray()
     {
         logger.AppendLine("DemoRandomNumbersInArray");
 
         // 4 randomly generated numbers between -10 and 10
         float[] randomNumbers = {
             Random.Range(-10f, 10f),
             Random.Range(-10f, 10f),
             Random.Range(-10f, 10f),
             Random.Range(-10f, 10f)
         };
 
         // each number in the array is random
         foreach (float randomNumber in randomNumbers)
             logger.AppendLine("  number > " + randomNumber);
         logger.AppendLine();
     }
 
     /*
         DemoKnownNumbersInArray
           index 0 > 100
           index 2 > 300
           index 2 > 300
           index 0 > 100
     */
     void DemoKnownNumbersInArray()
     {
         logger.AppendLine("DemoKnownNumbersInArray");
 
         // 4 chosen numbers, accessed randomly
         float[] knownNumbers = {
             100,
             200,
             300,
             500
         };
 
         // each access to the array is random
         for (int i = 0; i < 4; ++i)
         {
             int randomIndex = Random.Range(0, knownNumbers.Length);
             float randomNumber = knownNumbers[randomIndex];
             logger.AppendLine("  index " + randomIndex + " > " + randomNumber);
         }
         logger.AppendLine();
     }
 
     /*
         DemoKnownRangesInArray
           range[100, 150] > 121.7349
           range[200, 250] > 233.4365
           range[300, 350] > 335.7147
           range[500, 550] > 529.447
     */
     void DemoKnownRangesInArray()
     {
         logger.AppendLine("DemoKnownRangesInArray");
 
         // 4 chosen ranges of random numbers
         RandomRange[] knownRanges = {
             new RandomRange(100, 150),
             new RandomRange(200, 250),
             new RandomRange(300, 350),
             new RandomRange(500, 550)
         };
 
         // each range in the array generate a random number
         foreach (RandomRange knownRange in knownRanges)
             logger.AppendLine("  range " + knownRange + " > " + knownRange.Value);
         logger.AppendLine();
     }
 }
 
 [System.Serializable]
 public class RandomRange
 {
     public float min;
     public float max;
     public float Value { get { return Random.Range(min, max); } }
 
     public RandomRange(int min, int max)
     {
         this.min = min;
         this.max = max;
     }
 
     public override string ToString()
     {
         return "[" + min + ", " + max + "]";
     }
 }
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 acmeydan · Oct 11, 2015 at 10:14 PM 0
Share

Thank you very much. Yes, its little bit too much for me, trying to understand:) It'a great example, father of all examples:)

avatar image Statement acmeydan · Oct 11, 2015 at 10:32 PM 0
Share

It's good feedback :) Often the simplest answer is the best. I had problems understanding what you were asking so I pulled out the boomstick.

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

33 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

Related Questions

How to make certain elements in an array rarer when using random selection? 4 Answers

Getting Vector3 from random item in List 1 Answer

How do I sort floats by highest to lowest value? 1 Answer

Have random.range anims access float speed variable 0 Answers

Sample 3d float array 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