Created
May 24, 2024 18:11
-
-
Save yetimdasturchi/46de19c10d452400a146036d5ae196c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Tasofidiy son generatsiya qilish | |
function lcg(&$seed) { | |
//ushbu qiymatlar CSda tasodifiy son generatsiyasi uchun eng ko'p statistikalarga asoslanib aniqlangan | |
$a = 1664525; | |
$c = 1013904223; | |
// 2ning 32-darajasi | |
$m = pow(2, 32); | |
$seed = ($a * $seed + $c) % $m; | |
return $seed; | |
} | |
//Diapazon asosida tasofidiy son generatsiya qilish | |
function lcg_with_range(&$seed, $min, $max) { | |
$random_number = lcg($seed); | |
return $min + ($random_number % ($max - $min + 1)); | |
} | |
//kirish | |
$seed = 123; | |
//$seed = microtime(true); | |
for ($i = 0; $i < 10; $i++) { | |
echo lcg_with_range($seed, 0, 10) . "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function middleSquare($seed, $digits = 4) { | |
$numbers = []; | |
$numDigits = strlen((string)$seed); | |
// Kirish uzunligi juft ekanligiga ishonch hosil qilish | |
if ($numDigits % 2 != 0) { | |
echo "Kirish juft son bo'lishi lozim.\n"; | |
return; | |
} | |
for ($i = 0; $i < $digits; $i++) { | |
// Kavadratni hisoblash | |
$square = (string)($seed * $seed); | |
// Zarurat tug'ilganda kavadrat natijasining boshini 0 bilan to'ldirish | |
while (strlen($square) < $numDigits * 2) { | |
$square = "0" . $square; | |
} | |
// Markazdagi sonni olish | |
$start = (strlen($square) - $numDigits) / 2; | |
$seed = (int)substr($square, $start, $numDigits); | |
// Natijalarga qo'shish | |
$numbers[] = $seed; | |
} | |
return $numbers; | |
} | |
// Kirish | |
$seed = 12342; | |
$randomNumbers = middleSquare($seed, 10); | |
echo "Tasodify raqamlar: " . implode(", ", $randomNumbers) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment