# 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 ;
// Use this for initialization
void Start () {

for(int i=0; i<songs.Length; i++){
songs  *= 1;*
``````

_ Debug.Log(songs );_
* }*
* 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.

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)];
}
``````

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;

void Start()
{
for (int i = 0; i < songs.Length; i++)
{
songs *= 0; // I turned all to 0 for this example.*
``````

}

songs = songs = 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 usableIndecise = new List();
for (int i = 0; i < songs.Length; i++)
{
if (songs == value)