def randomized_motif_search(dna, k, t):
    best_motifs = []
    for seq in dna:
        pos = random.randrange(len(seq) - k + 1)
        best_motifs.append(seq[pos:pos + k])

    while True:
        profile = make_profile(best_motifs, k)
        motifs = [profile_most_prob(profile, seq, k) for seq in dna]
        if score_motifs(motifs) < score_motifs(best_motifs):
            best_motifs = motifs
        else:
            return best_motifs
    def run():
        best_motifs = []
        for seq in dna:
            pos = random.randrange(len(seq) - k + 1)
            best_motifs.append(seq[pos:pos + k])
        best_score = score_motifs(best_motifs)
        motifs = best_motifs[:]
        for _ in range(N):
            i = random.randrange(t)
            profile = make_profile(motifs[:i] + motifs[i + 1:], k)
            motifs[i] = profile_rand_kmer(profile, dna[i], k)
            score = score_motifs(motifs)
            if score < best_score:
                best_motifs, best_score = motifs[:], score

        print_motifs(best_motifs)
        return best_score, best_motifs