vendredi 3 novembre 2017

select array position from a list of booleans using a random number

I am working on musical application. I want to select notes randomly from the musical scale ( 12 notes ). I want the option to be able to choose a list of notes from within the 12 notes of the scale which are then randomly selected from. I thought searching through an array of booleans would do the trick. I wrote a little script in python 3 to test it out but can't get it to work .. Eventually I will port it to c++ ... Something like this . The for statement at the end doesn't work as I had hoped .. Perhaps this is a challenge someone might enjoy !

    import random
    numbers = []
    # array holding 12 notes of which I have 
    # made 5 "available" 
    numbers = [0,1,1,0,0,1,1,0,0,1,0,0]
    count = 0
    # count the number of notes "available"
    for i in range (0,11): 
        if numbers[i] == 1:
            count = count + 1 
    print (count)
    n = random.randint(0,count-1)
    print (n)
    j = 0
    p = 0
    # I was hoping to use the random number to 
    # select on of the available numbers 
    for x in range(0,11):
        if (numbers[x] == 1) and (j != n) :
            p = x 
            j = j + 1 
    print (p)
    # output doesn't look right ! 




Aucun commentaire:

Enregistrer un commentaire