Skip to main content
backwards compatibility for python file
Source Link

EDIT: use cryptographically secure os.urandom for true randomness

Python one-liner

You could install pythonpython and run the following command in your terminal

pythonpython3 -c 'import os; print(f"{int.from_bytes(os.urandom(1)):08b}")'

(might need to use python3 instead of python at the beginning). Here you get 8 coin flips at once.

Python file

For a bit more flexibility you can also save the following code as the file coinflips.py

#!/usr/bin/env python3

import os, sys

def flip_coins_n_times(n:int):
    nbytes = n//8 + bool(n%8)
    # get random bytes, convert to bits (including leading zeros)
    # finally remove excess and print
    print(f"{int.from_bytes(os.urandom(nbytes), 'big'):0{8*nbytes}b}"[0:n]) 

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(int(sys.argv[1]))

and run it from the command line as

python$ python3 coinflips.py 20
11100100011100100111
$ 

Here you can replace 20 by the number of coinflips you want

EDIT: use cryptographically secure os.urandom for true randomness

Python one-liner

You could install python and run the following command in your terminal

python -c 'import os; print(f"{int.from_bytes(os.urandom(1)):08b}")'

(might need to use python3 instead of python at the beginning). Here you get 8 coin flips at once.

Python file

For a bit more flexibility you can also save the following code as the file coinflips.py

#!/usr/bin/env python3

import os, sys

def flip_coins_n_times(n:int):
    nbytes = n//8 + bool(n%8)
    # get random bytes, convert to bits (including leading zeros)
    # finally remove excess and print
    print(f"{int.from_bytes(os.urandom(nbytes)):0{8*nbytes}b}"[0:n]) 

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(int(sys.argv[1]))

and run it from the command line as

python coinflips.py 20

Here you can replace 20 by the number of coinflips you want

EDIT: use cryptographically secure os.urandom for true randomness

Python one-liner

You could install python and run the following command in your terminal

python3 -c 'import os; print(f"{int.from_bytes(os.urandom(1)):08b}")'

Here you get 8 coin flips at once.

Python file

For a bit more flexibility you can also save the following code as the file coinflips.py

#!/usr/bin/env python3

import os, sys

def flip_coins_n_times(n:int):
    nbytes = n//8 + bool(n%8)
    # get random bytes, convert to bits (including leading zeros)
    # finally remove excess and print
    print(f"{int.from_bytes(os.urandom(nbytes), 'big'):0{8*nbytes}b}"[0:n]) 

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(int(sys.argv[1]))

and run it from the command line as

$ python3 coinflips.py 20
11100100011100100111
$ 

Here you can replace 20 by the number of coinflips you want

added 211 characters in body
Source Link

EDIT: use cryptographically secure os.urandom for true randomness

Python one-liner

You could install python and run the following command in your terminal

python -c 'import random as r;os; print(f"{rint.getrandbitsfrom_bytes(20os.urandom(1)):b08b}")'

(might need to use python3 instead of python at the beginning). You can replaceHere you get 208 with any number of coinflips you wantcoin flips at once.

Python file

For a bit more flexibility you can also save the following filecode as the file coinflips.py

#!/usr/bin/env python3

import sys
import random asos, rsys

def flip_coins_n_times(n:int):
    print(f"{r.getrandbits(nbytes = n)//8 :+ b}"bool(n%8)
    # printget random bytes, convert to bits (inincluding binaryleading zeros)
    # finally remove excess and print
    print(f"{int.from_bytes(os.urandom(nbytes)):0{8*nbytes}b}"[0:n]) 

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(int(sys.argv[1]))

and run it from the command line as

python coinflips.py 20

AgainHere you can replace 20 by the number of coinflips you want

Python one-liner

You could install python and run the following command in your terminal

python -c 'import random as r; print(f"{r.getrandbits(20):b}")'

(might need to use python3 instead of python at the beginning). You can replace 20 with any number of coinflips you want.

Python file

you can also save the following file as coinflips.py

#!/usr/bin/env python3

import sys
import random as r

def flip_coins_n_times(n):
    print(f"{r.getrandbits(n) : b}") # print random bits (in binary)

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(sys.argv[1])

and run it from the command line as

python coinflips.py 20

Again you can replace 20 by the number of coinflips you want

EDIT: use cryptographically secure os.urandom for true randomness

Python one-liner

You could install python and run the following command in your terminal

python -c 'import os; print(f"{int.from_bytes(os.urandom(1)):08b}")'

(might need to use python3 instead of python at the beginning). Here you get 8 coin flips at once.

Python file

For a bit more flexibility you can also save the following code as the file coinflips.py

#!/usr/bin/env python3

import os, sys

def flip_coins_n_times(n:int):
    nbytes = n//8 + bool(n%8)
    # get random bytes, convert to bits (including leading zeros)
    # finally remove excess and print
    print(f"{int.from_bytes(os.urandom(nbytes)):0{8*nbytes}b}"[0:n]) 

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(int(sys.argv[1]))

and run it from the command line as

python coinflips.py 20

Here you can replace 20 by the number of coinflips you want

Source Link

Python one-liner

You could install python and run the following command in your terminal

python -c 'import random as r; print(f"{r.getrandbits(20):b}")'

(might need to use python3 instead of python at the beginning). You can replace 20 with any number of coinflips you want.

Python file

you can also save the following file as coinflips.py

#!/usr/bin/env python3

import sys
import random as r

def flip_coins_n_times(n):
    print(f"{r.getrandbits(n) : b}") # print random bits (in binary)

if __name__ == '__main__':
    # file is run from commandline
    flip_coins_n_times(sys.argv[1])

and run it from the command line as

python coinflips.py 20

Again you can replace 20 by the number of coinflips you want