Random Number Generation, String Extraction, System Info, and Utility Functions

  • Share this:

Code introduction


Generate a random floating-point number within a specified range and round it to a specified number of decimal places. Extract numbers from a string, print system information, copy a dictionary, and delay execution


Technology Stack : random, math, re, sys, os, time, json, copy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import math
import re
import sys
import os
import time
import json
import copy

def generate_random_float(min_val, max_val):
    return random.uniform(min_val, max_val)

def round_float(num, decimals=2):
    return round(num, decimals)

def extract_numbers(text):
    return re.findall(r'\b\d+\.\d+\b|\b\d+\b', text)

def print_system_info():
    info = {
        'platform': sys.platform,
        'version': sys.version,
        'executable': sys.executable,
        'path': os.environ['PATH']
    }
    print(json.dumps(info, indent=4))

def copy_dict(original_dict):
    return copy.deepcopy(original_dict)

def delay_execution(seconds):
    time.sleep(seconds)