• Home
  • About

Shores of the Dirac Sea

A blog about physics… mostly.

Feeds:
Posts
Comments
« A public service announcement
Research doesn’t go as expected. »

Showing off code.

August 6, 2009 by dberenstein

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"

About these ads

Rate this:

Like this:

Like Loading...

Posted in computers | 9 Comments

9 Responses

  1. on August 6, 2009 at 7:03 pm proportional

    At the end shoud be fout.close()


    • on August 6, 2009 at 8:37 pm dberenstein

      Thank you:

      My machine doesn’t complain about this. I’m sure I have a typo there now.


  2. on August 6, 2009 at 11:41 pm carlbrannen

    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.


  3. on August 7, 2009 at 1:19 am David SD

    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.


  4. on August 7, 2009 at 5:01 am Luboš Motl

    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}, “1″ -> {1, 0}, “2″ -> {1, 0}, “3″ -> {0, 1},
    “4″ -> {0, 1}, “5″ -> {0, 0}};
    e = Flatten[d[[;; 300]]]

    Output:

    {{”3″, “0″, “5″, “0″, “3″, “3″, “0″, “0″, “5″, “1″}, ” and so on”}

    0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 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, 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


  5. on August 7, 2009 at 10:12 am Robert

    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);
    

  6. on August 7, 2009 at 10:13 am Robert

    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);
    

  7. on August 8, 2009 at 2:39 am Just Learning

    Inadequate randomness


  8. on August 8, 2009 at 1:39 pm Just Learning

    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



Comments are closed.

  • Recent Posts

    • Typos
    • Patterns
    • Woof Woof
    • Happy 3.1415926535… day
    • Unstable Universes
  • Archives

    • June 2013
    • May 2013
    • April 2013
    • March 2013
    • February 2013
    • January 2013
    • November 2012
    • September 2012
    • August 2012
    • July 2012
    • May 2012
    • March 2012
    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • September 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • August 2009
    M T W T F S S
    « Jul   Sep »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • Recent Comments

    dberenstein on Typos
    Mitchell Porter on Typos
    dberenstein on Typos
    Luboš Motl on Typos
    Plato on Woof Woof
  • Physics/Math/Science Blogs

    • Asymptotia (Clifford Johnson)
    • Backreaction
    • Coctail Party Physics
    • Cosmic Variance
    • Dmitry Podolsky
    • Jeffrey Epstein Science
    • John Baez
    • Michael Nielsen
    • Musings (Jacques Distler)
    • Not even wrong
    • Resonaances
    • Robert Helling
    • Shtetl Optimized
    • Sunclipse
    • Terry Tao
    • Tomasso Dorigo
    • Uncertain Principles
  • Science Resources

    • Physics (APS journal)
    • Scientific American
  • Some More Blogs

    • Evil Inc
    • Fafblog
    • phd Comics
    • Regator
    • Scenes from a multiverse
    • Site Meter
    • WordPress.com
    • WordPress.org
  • Pages

    • About
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.com

Blog at WordPress.com.

Theme: MistyLook by WPThemes.


Follow

Get every new post delivered to your Inbox.

Join 36 other followers

Powered by WordPress.com
%d bloggers like this: