2013-01-11 33 views
1

ho我讓隨機數字低於隨機數字之前。Python Random Random

if Airplane==1: 
while icounter<4: 
    ifuelliter=random.randrange(1,152621) 
    #litter/kilometer 
    LpK=152620/13500 
    km=LpK*ifuelliter 


    ipca=random.randrange(0,50) 
    ipcb=random.randrange(0,50) 
    ipcc=random.randrange(0,812) 


    #3D space distance calculation 
    idstance= math.sqrt((icba-ipca)**2 + (icbb-ipcb)**2 + (icbc-ipcc)**2) 

    totaldist=km-idstance 

    if totaldist>0: 
      print "You have enoph fuel to get to New York AirPort" 
      print ifuelliter,LpK,km,ipca,ipcb,ipcc,idstance 
      icounter=3 

    if totaldist<=0: 

     print "You dont have enoph fuel to get to New York AirPort please go to the nearest one or you will die" 
     print ifuelliter,LpK,km,ipca,ipcb,ipcc,idstance 
     icounter=icounter+1 

whati表示「ipca,ipcb,ipcc」,我需要他們會長下來而不是其他數字。

回答

1

剛剛成立randrange的第二個參數與前值:

import random 
a = random.randrange(0,50) 
b = random.randrange(0,a) 
while b > a: 
    b = random.randrange(0,a) 

順便說一句,要小心,如果你的代碼的開頭你的縮進風格

if Airplane == 1: 
while .... 

應該是

if Airplane == 1: 
    while ....