Could anyone help me in understanding what does below Python code do? Also please help me in converting it into Java.

import random

howMany = random.randint(0,1000)
stats = {}
for i in range(howMany):
   value = random.randint(0,500)
   stats.setdefault(value,0)
   stats[value]+=1
for item in stats:
   if stats[item] > 1:
       print item

Recommended Answers

All 10 Replies

A couple of extra print statements will help you.

IMHO the code is nonsense.

While running the code, I'm getting the following error at line 11:

Syntax error: invalid syntax

Is there any syntax error at line 11 ? print item ?

Is there any syntax error at line 11 ? print item ?

No, there isn't. I just ran all the code in python IDLE and got no errors.

I agree with HiHe, the code seems to be a bunch of non-sense. It apears to just be coming up with a bunch of random numbers, increasing them by one, and printing any that are greater than one up to a set limit. No real use that I can see with the information given.

As far as converting it to Java, it's been a while since I have even looked at Java code. Don't know if I can help you with that or not.

  • WolfSheild

Line 11 may give you error if you use Python3,
change to:
print(item)

As it is clear,

stats = {}

is a dictionary defined in the code. Is it an empty dictionary? Because generally it mst contain key and value pairs.

What does

stats[value]+=1

do? Can you tell me it's expanded form? Is it gonna be

stats[value]= value + 1

?

We generate 0-1000 (random) pieces of a random number between 0 and 500.
We count these random numbers in the "stats" dictionary. If a random number a is occuring n times then stats[a]==n.
We print out the random numbers that are counted more then 1 time in the stats dict.

The code isn't nonsense at all. It is generating a random number (between 0 and 1000) of random numbers (between 0 and 500) and storing statistics in a dictionary on how many times each number is generated.

First it determines how many numbers to generate by selecting a random number between 0 and 1000.

Then in the first for loop, value is assigned a random number between 0 and 500.
So as an example, on an iteration of the loop the number 497 is randomly generated.

As far as I understand it, the line: stats.setdefault(value, 0)
Will generate a key/value pair in the dictionary where 'value' is the key and 0 is the default value for the pair (so if the key/value pair does not already exist, it will be added to the dictionary and given a default value of 0). The final line in the first for loop increments the value of the key/value pair at stats[value] by 1.

So going back to the example using 497 as the generated number:
Assuming this is the first time that 497 is generated, the key/value pair 497,0 is added to the dictionary. So 497 is the key and 0 is the value. Then the value of the pair is incremented by 1. So 497 is the key and 1 is the value.

Any subsequent time the number 497 is randomly generated, setdefault will do nothing (because the key/value pair already exists in the dictionary) and the value part of the pair indexed by the key 497 will be incremented.

The final for loop in the program goes through all of the items in the dictionary and prints each number that was generated at least once.

If you change the final for loop to:

for item in stats:
    if stats[item]>1:
        print "Number: " + str(item) + ", times generated: " + str(stats[item])

Then you can see each number that was generated and the number of times it was generated.

So the code isn't nonsense and could potentially be useful, given the right application!

JasonHippy> was generated at least once.

No it is > 1, so those which was hit more than once.

I do not see the purpose of printing out the numbers that was hit multiple times. Seeing the distribution of counts would make more sense.

Ooops! Yes, you're right Tony... I stand corrected. :) Not sure how I missed that little detail... :/
Looks like I should have gone to specsavers, heh heh! XD

And I agree, there are probably several more interesting uses for the collected statistics on the generated numbers, but I stand by my original statement. My point was merely that the original code is not junk as stated by some of the earlier posters in the thread. It has a purpose, even if that purpose is somewhat limited in scope in terms of its usefulness!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.