随机和弦创造者与概率设置,不打印同一个音符twi

2024-03-28 09:49:09 发布

您现在位置:Python中文网/ 问答频道 /正文

创造一个和弦发生器的灵感来自太阳拉爵士音乐。我试图创建一个和弦发生器,可以非常定制。到目前为止,我可以给它提供注释,并在我的对象“note”中给这些注释设置概率。我遇到的问题是,当它生成序列时,我有时会得到相同的音符。是否有某种方法可以在每个打印行之间创建一个if语句,以排除从上一个呈现中选择的任意注释,从而使下一个注释不能与上一个注释相同?你知道吗

我试着在每一行之间写一个if语句,但是很尴尬,所以我不想分享。你知道吗

import random import numpy as np class Note: def __init__(self, name, note): self.name = name self.gender= np.random.choice(["c", "e", "g", "b"],1,[0.5, .2, 0.1, 0.2])[0] c = Note('c', 'yourNote') d = Note('d', 'yourNote') e = Note('e', 'yourNote') f = Note('f', 'yourNote') Your_Chord = Note(c.name, c.gender) print(Your_Chord) print(c.gender) print(d.gender) print(e.gender) print(f.gender)

Tags: nameimportselfyourifnprandom语句
1条回答
网友
1楼 · 发布于 2024-03-28 09:49:09

我认为你错过的主要事情是一些方法来跟踪什么是最新的注意,然后确保它没有被选中。我添加了一个类变量来跟踪最后一个音符,并删除了一个temp dict来从每次的音符中选取。我希望这对你疯狂的爵士乐有帮助!你知道吗

import numpy as np

# dict with the notes and weights
notes = {"c":0.5, "e":0.2, "g":0.1, "b":0.2}

class Note:
    _last_note = 'start'
    def __init__(self, name, note):
        self.name = name
        # here we temporarily create a dict without the last note in it
        new_notes = {k: notes[k] for k in set(list(notes.keys())) - set(Note._last_note)}
        self.gender = np.random.choice(list(new_notes.keys()-Note._last_note),1,list(new_notes.values()))[0]
        print("init ",self.gender,Note._last_note)
        Note._last_note = self.gender

    def print_chord(self):
        print("print chord ",self.name,self.gender)



c = Note('c', 'yourNote')
d = Note('d', 'yourNote')
e = Note('e', 'yourNote')
f = Note('f', 'yourNote')
Your_Chord = Note(c.name, c.gender)

Your_Chord.print_chord()

print(c.gender)
print(d.gender)
print(e.gender)
print(f.gender)

c.print_chord()
d.print_chord()
e.print_chord()
f.print_chord()

相关问题 更多 >