Skip to content
The Chat Archive
minimax-m1 LMArena Human Preference 140K

i want 4 random partitions

  1. User

    i want 4 random partitions

  2. Assistant

    Here's the modified program with 4 random partitions (ensuring total ≤ 200):

    import random  # Added to generate random partitions
    
    # Prompt user for algorithm choice
    print("Select a memory allocation algorithm:")
    print("1. First Fit (.❛ ᴗ ❛.)\n")
    print("2. Best Fit⸜( ´ ꒳ ` )⸝\n")
    print("3. Worst Fit(¬‿¬ )\n")
    
    algo_choice = int(input("Enter 1, 2, or 3: "))
    
    # Generate 4 random partitions (total ≤ 200)
    partitions = []
    total_size = 0
    
    print("\nGenerating 4 random partitions (total ≤ 200)...")
    while True:
        # Generate 4 random sizes between 1-200
        sizes = [random.randint(1, 200) for _ in range(4)]
        total_size = sum(sizes)
        if total_size <= 200:
            break  # Valid partition set
    
    # Add generated partitions to list
    for i, size in enumerate(sizes, 1):
        partitions.append({"size": size, "status": "free", "job": None})
    
    # Show generated partitions
    print("\nGenerated Partitions:")
    for i, p in enumerate(partitions, 1):
        print(f"Memory {i}: {p['size']}k")
    
    # Fixed jobs as per requirement
    jobs = [
        {"job_no": 1, "size": 30, "allocated": False, "partition": None},
        {"job_no": 2, "size": 50, "allocated": False, "partition": None},
        {"job_no": 3, "size": 30, "allocated": False, "partition": None},
        {"job_no": 4, "size": 25, "allocated": False, "partition": None}
    ]
    
    # Function to allocate jobs (same as before)
    def allocate_jobs():
        for job in jobs:
            job_size = job["size"]
            allocated = False
            selected_partition = -1
    
            # Check if job can fit in any partition
            max_partition = max(partitions, key=lambda x: x["size"])["size"]
            if job_size > max_partition:
                print(f"Job {job['job_no']} (size {job_size}) is too big for any partition")
                continue
    
            # Allocation based on First Fit
            if algo_choice == 1:
                for i in range(4):
                    if partitions[i]["status"] == "free" and job_size <= partitions[i]["size"]:
                        selected_partition = i
                        allocated = True
                        break
    
            # Allocation based on Best Fit
            elif algo_choice == 2:
                min_diff = float('inf')
                for i in range(4):
                    if partitions[i]["status"] == "free" and job_size <= partitions[i]["size"]:
                        diff = partitions[i]["size"] - job_size
                        if diff < min_diff:
                            min_diff = diff
                            selected_partition = i
                            allocated = True
    
            # Allocation based on Worst Fit
            elif algo_choice == 3:
                max_size = -1
                for i in range(4):
                    if partitions[i]["status"] == "free" and job_size <= partitions[i]["size"]:
                        if partitions[i]["size"] > max_size:
                            max_size = partitions[i]["size"]
                            selected_partition = i
                            allocated = True
    
            # Allocate job if possible
            if allocated:
                partitions[selected_partition]["status"] = "busy"
                partitions[selected_partition]["job"] = job["job_no"]
                job["allocated"] = True
                job["partition"] = selected_partition + 1
    
    # Call the allocation function
    allocate_jobs()
    
    # Display memory partitions summary
    print("\nMemory Partitions Summary:")
    for job in jobs:
        if job["allocated"]:
            print(f"JobNo.{job['job_no']}: Job_size {job['size']}: Allocated, Partition {job['partition']}")
        else:
            print(f"JobNo.{job['job_no']}: Job_size {job['size']}: Not Allocated")
    
    # Display allocation results table
    print("\nAllocation Results Table:")
    print("Job\tJob Size\tPartition\tPartition Size\tStatus\tInternal Fragmentation")
    for job in jobs:
        job_no = job["job_no"]
        job_size = job["size"]
        if job["allocated"]:
            partition = job["partition"]
            partition_size = partitions[partition-1]["size"]
            status = "Allocated"
            fragmentation = partition_size - job_size
        else:
            partition = "-"
            partition_size = "-"
            status = "Waiting"
            fragmentation = "-"
        
        print(f"{job_no}\t{job_size}\t\t{partition}\t\t{partition_size}\t\t{status}\t{fragmentation}")
    

    Key changes:

    • Added import random to generate random partitions.
    • Replaced manual partition input with a loop that generates 4 random sizes (sum ≤ 200).
    • Shows the generated partitions to the user before allocation.

    Now the program will automatically create 4 random partitions (e.g., [45, 80, 30, 45] if sum ≤ 200) and proceed with allocation.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.