How to Write a Program to Generate Random Numbers
- 1). Open a blank text file in an application that can save files in ASCII. Most likely your operating system came with a lightweight program that works with plain ASCII files, but you could use a full-blown word processor like Word as long as you remember to save the file in ASCII.
- 2). Type "import random" as the first line of the file. This statement tells the Python interpreter you intend to use the part of the programming language that deals with random numbers.
- 3). Enter "print random.randint(1, 10)" as the second line. This statement calls the function that does all the heavy lifting of random number generation and then prints that value to the screen. In a real-world production environment you would instead assign the random number returned by randint() to a variable for further processing.
- 4). Verify that you have entered the statements correctly and close the file after saving it. Give the file any name you want but make sure it has a ".py" file extension. Save it as an ASCII file.
- 5). Run the file by typing "python" followed by the file name you specified previously at an operating system console window. Your OS probably allows you to run the program by double-clicking its icon via the GUI, but since it sends output to the console, its window might open and close too fast for you to see that output.