954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array random integers with step given by the user

Hi!
My aim is to create a grid in which cols, rows, range, and step are given by the user BUT they have to be placed in the grid in a random way. Then, I have to save that grid in a txt file. I wrote the following using some inputs found around but I got stuck.
Essentially, I cannot combine random and step codes.
Thanks in advance for your help!

import random
import sys
import os.path
from numpy import *
from numpy.random import *

rows = input("Input the number of rows: ")
cols = input("Input the number of columns: ")
range_start = input("Specify the start of the range: ")
range_stop = input("Specify the stop of the range: ")
step = input("Specify the step: ")

def toAscii(name, body):
    """ toAscii(nameString, headerList, body2DList) -> None """
    # open a file
    fname = name + ".txt"
    print "Creating output ASCII file", fname,
    f = open(fname,'w')
    # create the body content
    contentString = ""
    for steP in body:
        line = " ".join(str(col) for col in steP) + "\n"
        contentString = contentString + line
    # saving body
    f.write(contentString)
    f.close()
    print "... Done!"
    
if __name__ == "__main__":
    # test *****
    #row = range(range_start, range_stop)
    steP = range(range_start, range_stop, step)
    print steP
    print "the content to be saved:"
    for line in steP:
        print line
    toAscii("user", steP)
henky2011
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You basically look like doing nothing only outputting simple range to file. What is purpose of those import statements in begining, I see no reference to any module.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Your description sounded like you want to do something like:

import random
input = raw_input
    
if __name__ == "__main__":
    rows = int(input("Input the number of rows: "))
    columns = int(input("Input the number of columns: "))

     with open("user.txt",'w') as out:
        start, end = int(input("Specify the start of the range: ")), int(input("Specify the stop of the range: "))
        step = int(float(end-start)/rows/columns)+1
        width = len(str(end))

        my_range = range(start, end+1, step)
        random.shuffle(my_range)
        for count, value in enumerate(my_range):
            print '%*s' % (width, value),
            print >>out, '%*s' % (width, value),
            if columns - 1 == count % columns:
                print
                print >>out
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

You are great! Thank you so much! There's only one thing: even the STEP has to be specified by the user. I tried to do it in your script but without success! :-(

henky2011
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

BUG: +1, at line 10 should go after -start.

step = int(float(end-start+1)/rows/columns)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: