r/AskProgramming • u/ki4jgt • 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
26
u/Numerous-Match-1713 Jul 16 '26
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.
6
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.
-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.
-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