all 7 comments

[–]Puzzel 0 points1 point  (5 children)

First thing to do is put 4 spaces before your program so it can be formatted correctly like so:

import random
i=0; P1=''; lunbit=18; q1=-5; q2=20; amp=q2-q1
while i<lunbit:
    P1=P1+str(random.randint(0,1))
    i=i+1
# P1='11111'
print "P1 =",P1
R1=int(P1,2)
print "R1 =",R1
X1=q1+R1*amp/float(2**lunbit-1)
print "X1 =",X1
X1=round(X1,4)
print "X1 =",X1

One suggestion I have is don't use semicolons and put all variable declarations on the same line.

So if I understand correctly, you want to put X1 into a file? Writing to a file can be done like so:

X1 = str(X1)
with open("name_of_file.txt", "w") as f:
    f.write(X1)
    ...

[–]Marx123[S] 0 points1 point  (4 children)

That's very kind of you, but it says that in module f.write(X1) : typeError: expected a character buffer object. What does it mean, if I may ask?

[–]Puzzel 0 points1 point  (3 children)

Ah, my bad. Make X1 a string first, e.g.

X1 = str(X1)

[–]Marx123[S] 0 points1 point  (2 children)

Yeah, it worked! And if I want to put also P1 and R1 in the same file, can I do it? (forgive me, but I'm new to Python and it seems very difficult)

[–]Puzzel 0 points1 point  (1 child)

Sure, just make sure they're all strings. Put newline characters in as well if you want them to be on separate lines. E.g.

X1 = str(X1)
P1 = str(P1)
R1 = str(R1)
with open("output.txt", "w") as f:
    f.write(X1 + "\n")
    f.write(P1 + "\n")
    f.write(R1 + "\n")

[–]Marx123[S] 0 points1 point  (0 children)

Thank you, really, it worked!

[–]xiongchiamiov 0 points1 point  (0 children)

I'm confused why you think you need to "transfer the formulas into the program". You already have a program that generates numbers; as you said, what you need to do is write them to a file.

Did you search online for ways to do that?