简体   繁体   中英

Integer Division with random.randint

When using random.randint() in this form, I can't get integer division to work:

(random.randint(-40, 215) - -40) // (215 - -40)

This returns 0, however something like this returns correctly:

random.randint(3,5) - 2 // 2

How do I format the first equation correctly to get a rounded integer instead of defaulting to 0?

In case someone runs into this when normalizing integers, here is a normalization between -40 and 215:

(random.randint(-40,215) - -40.0) * 10 // (215.0 - -40.0) * 10

Kudos to Willem Van Onsem

In case you want the non int answer

print (random.randint(-40, 215) +40.0) / (215+40)

The result lies always between (0,1) step of which is always 0 and hence it returns always zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM