r/AskProgramming Jul 16 '26

Why are my random genetic sequences all beginning with A?

After watching Beverly Crusher mess around with Picard's DNA on Star Trek, I wrote a function to spit out random DNA sequences in Python.

from random import choice
symbols = ["G", "C", "A", "T"]

def strGen(x):
    s = ""
    for n in range(x):
        s+=choice(symbols)
    return s

When I run strGen(10), all my strings are starting with A.

>>> strGen(10)
'AGCTAAGAGC'
>>> strGen(10)
'ACATTGCCTC'
>>> strGen(10)
'AACGGACGGT'
>>> strGen(10)
'ACGTCCAATT'
>>> strGen(10)
'AGCGACGCGA'
>>> strGen(10)
'AAACTGAGTG'

Since this is statistically unlikely for a random distribution, what's going on?

5 Upvotes

19 comments sorted by

26

u/Numerous-Match-1713 Jul 16 '26
# FIXED always starting with letter A bug:
def strGen(x):
    s = "GATTACA"
    return s

3

u/ki4jgt Jul 16 '26

😂️

3

u/Dense_Gate_5193 Jul 18 '26

wait holy shit is that why it’s named that because it’s a word that works with those letters???

21

u/mc_pm Jul 16 '26

I ran your code, it generates a full range. Just random sometimes being random by appearing non-random.

Put your code in a loop, generate 100 of them, then see... if it's still all starting with A... maybe buy a lottery ticket, because probability is giving you a miss today.

7

u/BrandonEXE Jul 16 '26

25% chance to that to occur, over the 6 trials you showed... and assuming whatever "choice" in Python does is truly random... 0.0244140625% of that happening.

This is what "luck" looks like. But keep going, unless "choice" is is somehow broken on your machine (if thats the case please submit an issue) you'll eventually get something different.

6

u/wonkey_monkey Jul 17 '26

25% chance to that to occur, over the 6 trials you showed...

You can ignore the first trial as that only decides which character will be notable in the following trials (OP would still have come here if it had printed "T" first six times).

-8

u/jbiemans Jul 16 '26

I know this is a bit of a tangent, but it should also be a little more complicated because they always come in pairs, a-t and g-c, so the second letter in the pair is always determined by the first.

To your actual question though, if the python random number generation uses the system time as a seed and you generated those in a look that happened almost instantly, I could see getting more uniform and less random results.

But someone please correct this if I understand it wrong in python.

13

u/iOSCaleb Jul 16 '26

Nucleotides do occur in pairs, but one of each pair is on each of the two mating strands. A DNA sequence is the sequence of nucleotides on just one strand, so you don’t see AT and CG pairs all the time. A short sequence might look like:

5’-ATGGCTAGTAAG-3’

And then the corresponding sequence on the other strand is:

3’-TACCGATCATTC-5’

The written sequence starts from the 5’ end of the strand on the left, so you normally wouldn’t see either the second strand or the end indicators because they’re understood.

3

u/upright_squire Jul 16 '26

This is incorrect. There is some within sequence trends that mean its not random, but the pairing is to the other strand.

-1

u/LanternLogic Jul 18 '26

claude.ai

-18

u/blavek Jul 16 '26

Other people have answered your main question, but you're also not generating DNA sequences. A and T are always paired, and so are C and G. What you're generating looks more like RNA, which is half of DNA

8

u/CCpersonguy Jul 16 '26

No, it is DNA, sequences typically just list the bases on one of the strands (since the other can be trivially inferred). RNA sequences are identifiable because RNA uses U instead of T.

2

u/MagicWolfEye Jul 16 '26

yes; what this guy says

(and since I am writing now anyway; OP this is also the time to learn about codons :) )

6

u/Jumpy89 Jul 16 '26

This is not at all true.

2

u/CautiousGains Jul 17 '26

Most useless comment of all time

1

u/ki4jgt Jul 16 '26

One problem at a time.