- Home /
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.
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)];
}
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.
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.
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)];
}
}
Your answer
Follow this Question
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