user13355479
user13355479

Reputation:

Random Increasing Number

I am trying to make a function(or just a piece of code) that will count from 0 to 100, while each number that the program will print will be greater than the previous one (like this:0,14,20,21,26,34,58,..100).What I made is just a function that will increase the number by 1 every 1 second.

    import time,random
def inumber(count):
    while count!=101:
          time.sleep(1.0)
          (random.randrange(0, 100))
          count=count+1;
          print(count)
inumber(0)

Upvotes: 0

Views: 340

Answers (2)

João Pinto
João Pinto

Reputation: 5619

You do not want to get a random number between 0 and 100 at each iteration, instead you want to adjust your counter to a random number between the current number+1 and 101 (the limit for randrange() is the expect max number + 1).

import time
import random


def inumber(count):
    print(count)  # Print first number
    while count < 100:
        time.sleep(1.0)
        # Set count to a number between the next number and 101
        count = random.randrange(count+1, 101)
        print(count)


inumber(0)

Upvotes: 1

Błotosmętek
Błotosmętek

Reputation: 12927

import time,random
def inumber(count):
    while count<=100:
        time.sleep(1.0)
        count += random.randrange(0, 100) # consider a number lower than 100 here
        print(count)
inumber(0)

Upvotes: 0

Related Questions