-2

I have a list of urls, something like

urls = [a long list of urls]

Now I am obtaining a random integer between 0 and length of the list as follows.

rand = random.randint(0, len(urls))

Now lets say I have two methods to call say method1() and method2() and I want to generate some sort of load on them.

So first time I want one url and then call method1() and method2() and get the time, next time two urls from the list and then call both the methods, the third time with three urls and so on. Finally I want to plot the time it took to execute the method with one url, with two url and so on.

How can I do this.

  • http://stackoverflow.com/questions/9690009/pick-n-items-at-random – akonsu Feb 05 '15 at 17:19
  • @akonsu OP wants something wierd... probably to measure time to fetch data from two.. three... four...( and so on...) url's randomly selected from the list. – sarveshseri Feb 05 '15 at 17:21
  • Given you have a finite list (you're calling `len` on it), put together `random.sample` and `time.clock`? – Ulrich Schwarz Feb 05 '15 at 17:21
  • @SarveshKumarSingh one part of the question is to pick a few urls at random. The link provides a solution to that. I do not know about the rest of the question. – akonsu Feb 05 '15 at 17:23

1 Answers1

1

As the problem statement says that, first time I want one url, next time two urls from the list and so on so we use a for loop which defines the number of urls to be fetched each time, Then we use a while loop to fill the all_urls list with random unique urls and that's why we used if not rand_url in all_urls: a while loop is preferred because It will keep iterating until we get the specified number of urls.

import random
for i in xrange(len(urls)):
    all_urls = []
    count = i
    while (count>=0):
        rand_url = urls[random.randint(0,len(urls)-1)]
        if not rand_url in all_urls:
            all_urls.append(rand_url)
            count-=1
    print all_urls

The output of this code would be something like :

['url_1']
['url_2', 'url_1']
['url_1', 'url_3', 'url_2']
... and so on 

And here is more simplified version of the above code, it will also land you on the same output:

import random
for i in xrange(1,len(urls)):
    print random.sample(urls,i)
ZdaR
  • 22,343
  • 7
  • 66
  • 87