Created
November 8, 2012 18:49
-
-
Save yledu/4040720 to your computer and use it in GitHub Desktop.
Simple random number output to a file, as in http://blog.cdleary.com/2012/06/simple-selfish-and-unscientific-shootout/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function writeStuff(n) | |
f=open("data.txt","w") | |
for el in 1:n | |
r=randi(Int32) | |
println(f,r) | |
end | |
close(f) | |
end | |
N = 1000_000 | |
tic();writeStuff(N);toc() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
def writeStuff(n): | |
random.seed() | |
with open('data.txt', 'w') as file: | |
for i in xrange(n): | |
r = random.getrandbits(31) | |
print >> file, r | |
file.close() | |
N = 1000000 | |
writeStuff(N) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function writeStuff(n) | |
f=open("data.txt","w") | |
for el in 1:n | |
r="12" | |
println(f,r) | |
end | |
close(f) | |
end | |
N = 1000_000 | |
tic();writeStuff(N);toc() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def writeStuff(n): | |
with open('data.txt', 'w') as file: | |
for i in xrange(n): | |
r = "12" | |
print >> file, r | |
N = 1000000 | |
writeStuff(N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
close()
at the end of thewith
block is not needed, and indeed not idiomatic, as it's implied by leaving the context of thewith
block.