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 banana111 · Apr 02, 2017 at 10:08 AM · arrayrandomarraysrandom.rangeint

pick a random int with the value of 1 from an array

I have this script that sets all the int in the 12 int long array to 0:

 int[] songs = new int [12];
     // Use this for initialization
     void Start () {
         
         for(int i=0; i<songs.Length; i++){
             songs [i] = 1;
             Debug.Log(songs [i]);
         }
         Set ();
     }


how do I set a int to a ranomd int from the array that has the value of 1 (int his cause it would choose from all of them cause they're all set to 1) something like this:

  int random_pick = random number from songs that is = to 1

for eg. it would pick the 3rd int and the int random_pick would be equal to 3. thanks for your help in advanced.

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
0

Answer by sleepandpancakes · Apr 04, 2017 at 05:09 AM

If I understand correctly, you want to find all the indices where which the array element is equal to 1, then pick a random index from those indices. This code is untested but I think it should get you what you need.

 void GetRandomElementWithValue(int value)
 {
     int[] indices = Array.FindAll(sounds, x => x == value);
     int randomIndex = indices[Random.Range(0, indices.Length)];
 }
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 NoseKills · Apr 04, 2017 at 03:57 PM 1
Share

I suspect this doesn't work as intended. FindAll() returns an array with all the elements in the source array that fulfill the predicate (not the indices of those elements) -> it'll be an array with as many 1s as there were in the source array.

If the source array has only one "1" left at let's say index 5 indices will be an array with one "1", and randomIndex will become that 1.

avatar image sleepandpancakes NoseKills · Apr 04, 2017 at 04:34 PM 0
Share

Oh right, I forgot about that. In that case, @banana111, you could either just make a List for the indices and iterate through the array, or you could use the method to get an array of all the indices as described in this page.

avatar image
0

Answer by CoryButler · Apr 04, 2017 at 05:29 PM

Store the index of the songs that meet your criteria in a list. Then, select an random item from that list.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 public class RandomSong : MonoBehaviour {
 
     int[] songs = new int[12];
 
     void Start()
     {
         for (int i = 0; i < songs.Length; i++)
         {
             songs[i] = 0; // I turned all to 0 for this example.
         }
 
         songs[9] = songs[11] = 1; // Then, I turned only these to 1.
 
         int random_pick = 0;
         if (songs.Any(x => x == 1)) // Make sure at least one song = 1.
             random_pick = GetRandomSongWithValue(1);
 
         Debug.Log(random_pick); // This will be either 9 or 11.
     }
 
     int GetRandomSongWithValue(int value)
     {
         // Make a list of the indecise of all songs with a value of 1.
         List<int> usableIndecise = new List<int>();
         for (int i = 0; i < songs.Length; i++)
         {
             if (songs[i] == value)
                 usableIndecise.Add(i);
         }
 
         // Return a random index.
         return usableIndecise[UnityEngine.Random.Range(0, usableIndecise.Count)];
     }
 }
 
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you get different random numbers for each object array? 1 Answer

How to know what random number is chosen 2 Answers

An array of bools with random true and false elements 2 Answers

Selection list from Array Unity - Random - GameObjects array 1 Answer

My sprite doesn't render on scene view and game view 2 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