Here is the test code I wrote with proper indentation, and that gave the output posted as a puzzle here. I’d like to thank Rod for pointing out this feature to me.
The whole idea was to get acquainted with basics of python. So I wanted to generate some random data, use some conditionals, use one external package (in this case random), to have at least one loop, get input from the keyboard, print out the data, both on screen and to a file and format the data on the file for more ease of reading.
I was purposefully sloppy with not verifying the data, nor converting it to float values, to see how it would read it. It guessed correctly with the input I gave it. The file is controlled by fout that opens the file ‘data.txt’ for output. It is an object that lets you write to it. It’s really not a bad first program, for a beginner MCMC code. Notice that there are no type declarations. These would have made the equivalent c code longer.
import random
s2= input("Input bias: a number between 0 and 1")
digits1= input("How many digits?")
s= random.random()
if s<0.5:
x=0
else:
x=1
fout=open("data.txt", 'w')
j=0
while j<digits1:
s= random.random()
if s < s2:
x+=1
x%=2
print x,
fout.write((str(x)+','))
if (j%30)==29:
fout.write('\n')
j+=1
fout.close()
print "\n" "Exit complete"

At the end shoud be fout.close()
Thank you:
My machine doesn’t complain about this. I’m sure I have a typo there now.
No type declarations??? That’s about the only thing that keeps me from writing gibberish. I use Java which is fully typed, as far as I can tell, but for years I used Fortran which needed little.
Wonderful to see more physicists using languages that aren’t many decades behind the cutting edge.
carlbrannen: Python is much more expressive than Java in that you usually need less, simpler, and more readable code to do what you want. This cuts down on bugs in its own way. I agree that statically typed languages can be a huge help for avoiding bugs. But type declarations aren’t necessary for that advantage. Check out type inference in OCaml or Haskell.
In order to restore some self-confidence for us the monovacuists, here is a Mathematica code that generates a very similar sequence, with equally represented binary digits 0,1, but with consecutive pairs 0,1 and 1,0 being twice as frequent as 0,0 or 1,1.
The code is much shorter and and has a unique, non-random answer, unlike David’s.
I took pi in the base-6 system (3.0503…, but any irrational number would do), and made the maps from the digits 012345 to 11,10,10,01,01,00 that are obvious in the text below. I have even managed to agree with the first six digits of David, by making appropriate choices (”inverse” map).
a = BaseForm[N[Pi/6, 250], 6];
b = ToString[a];
c = Characters[b][[3 ;; 302]]; {c[[;; 10]], ” and so on”}
d = c /. {”0″ -> {1, 1}, “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, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1
Here we go in perl:
#!/usr/bin/perl $|=1; # Prevent buffering for interactive IO. print "Input bias: A number between 0 and 1:"; $b = ; print "How many digits? "; $d = ; open(FOUT, ">data.txt"); # bad habit not to check result! $x = rand()<$b ? 0 : 1; for $j(1..$d){ if (rand() < $b){ ++$x; $x %= 2; } print FOUT "$x,"; print FOUT "\n" unless $j % 30; } close(FOUT);it ate my brackets! here we go again:
#!/usr/bin/perl $|=1; # Prevent buffering for interactive IO. print "Input bias: A number between 0 and 1:"; $b = ≤≥; print "How many digits? "; $d = ≤≥; open(FOUT, ">data.txt"); # bad habit not to check result! $x = rand()<$b ? 0 : 1; for $j(1..$d){ if (rand() < $b){ ++$x; $x %= 2; } print FOUT "$x,"; print FOUT "\n" unless $j % 30; } close(FOUT);Inadequate randomness
No this doesn’t make an external data call, but I like Visual Basic and random loops
Sub Macro1()
Dim iterations
iterations = 100
For Counter = 1 To iterations
Set curCell = Worksheets(“Sheet1″).Cells(Counter, 1)
If curCell.Value < 2 Then curCell.Value = Int(2 * Rnd)
Next Counter
End Sub