You can download this code by clicking the button below.
This code is now available for download.
This function accepts an HTTP request and returns a JSON response containing a random number between 1 and 100.
Technology Stack : Starlette (used to create Web APIs), random (used to generate random numbers)
Code Type : Web API Function
Code Difficulty : Intermediate
from starlette.responses import JSONResponse
from starlette.requests import Request
import random
def random_number_generator(request: Request):
# Generate a random number between 1 and 100
random_number = random.randint(1, 100)
return JSONResponse({"random_number": random_number})
# JSON Explanation