[Tutor] generate random number list and search for number

Quiles, Stephanie stephanie.quiles001 at albright.edu
Tue Aug 11 05:18:59 CEST 2015


Hello everyone,

Totally lost here. i have to create a program that asks user to input a value and then search for this value in a random list of values to see if it is found. Please see below for more detailed info.


This problem requires you to use Python. We want to test out the ordered sequential search algorithm (Listing 5.2). Here’s how we will do it. We’ll need to have a routine that will generate some random numbers for us to place into a list. We’ll need to have a main function to drive the program. And we’ll need to have Listing 5.2 to actually run through our list to check for some value that we will be looking for.
•       First, we’ll need to import time to use the clock routine, and we’ll need to use the randrange routine from random.
•       Next, we’ll want our main( ) routine to call our createRandnums( ) routine and have this routine return our list of random numbers. Once we have the list, we’ll start the time clock and then pass the list, along with a value to “search for” to the orderedSequentialSearch() routine. This routine will return True or False as to whether or not it found the value in the list. Once the routine has completed, we’ll want to stop the clock in main. By subtracting “end – start”, we’ll be able to figure out how long the routine took to run.
•       The createRandnums routine will accept two parameters (how many numbers do you want, and what is the upper limit on the randrange routine), and this will simply run a loop to create the numbers and append them to a list. Once the list is complete, we can then use the sort routine on the list to arrange the numbers in order. The function will now return the sorted list to main( ). The main( ) routine will then record the clock time, call the search routine, receive back a True or False value from the search, stop the clock, and print our whether or not the value was in the list, and how long the search took.
Some comments: why am I placing an upper bound on the randrange function? Well, consider if we want to generate 100 random numbers. If we run a loop 100 times, and each time through the loop we say something like x=randrange(1,101), we’ll be getting numbers from 1 – 100 inclusive. Now since the loop will run 100 times, and we’re only picking numbers from 1 – 100, we’re bound to get a number of repeats in the list. But if we, say, still run the loop 100 times, but we say x=randrange(1,300), we’ll still be generating 100 numbers (because the loop runs 100 times), but our spread of numbers should be better.
We’ll need to figure out what value to look for in the list. We could ask the user to supply a value.
You need to run the program for lists of 100, 1000, and 10000 random numbers.

      A sample of how you might proceed follows:
      This program will generate a list of random numbers and
test the sequential search algorithm
Enter number of random integers to create in list: 100
Enter high-end random number to generate: 300
Enter number to find within list: 23

Here are the results:
Item to find in random list of 100 values:  23
Sequential search: Found= False  Time: 9.91956746077e-006

Here is the thing I am totally lost. Here is what i have so far… i created the main function and i ask the user to search for a number. I have assigned a range of 100. but here is one of my questions how do i assign to search within a value range(for example, i want the program to spit out 100 numbers between 1-300). how would i go about doing this? also how do i call the orderedSequentialSearch so i can check to see if value is in list?

import time
import random

def orderedSequentialSearch(alist, item):
    pos = 0
    found = False
    stop = False
    while pos < len(alist) and not found and not stop:
        if alist[pos] == item:
            found = True
        else:
            if alist[pos] > item:
                stop = True
            else:
                pos = pos + 1

    return found

def main():
    list_range = 100
    find = int(input("enter search number: "))
    for num in range(1, list_range + 1):
        if random.randint(1, list_range) != 0:
            print(random.randint(1, list_range))
            if random.randint(1, list_range) == find:
                print("Number Found.")
            else:
                print("not found")

if __name__ == '__main__':
    main()

 Thank you so much in advance!

Stephanie







More information about the Tutor mailing list