🎲 random - Rastgele int Üretimi

RNG, Distributions, Sampling, Shuffle

44
function
598
lines
19 KB
Boyut

🚀 Quick Start

import random

// Random integer [0, 100)
let x = random.aralık(0, 100)

// Random float [0.0, 1.0)
let f = random.float()

// Random boolean
let b = random.bool()

// Random choice
let renkler = ["kırmızı", "yeşil", "mavi"]
let seçilen = random.seç(renkler)

// Shuffle
let deste = [1, 2, 3, 4, 5]
random.karıştır(deste)

// Seed (reproducible)
random.seed(42)
yazdır(random.aralık(0, 100))  // each zaman aynı Result

// Distributions
let normal = random.normal(0.0, 1.0)  // μ=0, σ=1
let uniform = random.uniform(10.0, 20.0)
let exponential = random.exponential(1.5)

📚 RNG Algoritmaları

💡 Exampleler

Monte Carlo Simülasyonu (Pi Tahmini)

import random, math

function pi_tahmini(numune_sayısı: tamsayı) -> float do
    let daire_içi = 0
    
    each i in aralık(0, numune_sayısı) for do
        let x = random.float()
        let y = random.float()
        
        // Nokta birim daire in mi?
        if (x*x + y*y) < 1.0  do
            daire_içi = daire_içi + 1
        end
    end
    
    // Pi ≈ 4 * (daire içi nokta sayısı / toplam nokta sayısı)
    return 4.0 * daire_içi.float() / numune_sayısı.float()
end

let tahmin = pi_tahmini(1000000)
yazdır("Pi tahmini: " + tahmin.yazıya())
yazdır("Gerçek Pi: " + math.PI.yazıya())
yazdır("Error: " + math.abs(tahmin - math.PI).yazıya())

Weighted Random Selection

import random

function weighted_choice(items: Dizi, weights: Dizi) -> herhangi do
    // Kümülatif ağırlıklar
    let total = 0.0
    let cumulative = []
    
    each w in weights for do
        total = total + w
        cumulative.ekle(total)
    end
    
    // Random seçim
    let r = random.float() * total
    
    each i in aralık(0, cumulative.uzunluk()) for do
        if r < cumulative[i]  do
            return items[i]
        end
    end
    
    return items[items.uzunluk() - 1]
end

// Example: Loot sistemi (oyun)
let loot = ["Common", "Uncommon", "Rare", "Epic", "Legendary"]
let chances = [50.0, 30.0, 15.0, 4.0, 1.0]  // %

let bulunan = do end
each i in aralık(0, 100) for do
    let item = weighted_choice(loot, chances)
    
    if bulunan.içerir_mi(item)  do
        bulunan[item] = bulunan[item] + 1
    end else do
        bulunan[item] = 1
    end
end

each (item, count) in bulunan for do
    yazdır(item + ": " + count.yazıya() + " (" + (count.float()).yazıya() + "%)")
end

← All Modules