[PHP] ขอวิธีการสุ่มตัวเลขไม่ซ้ำ แบบประหยัด memory หน่อยครับ

เริ่มโดย xmen256k, 29 ตุลาคม 2013, 18:23:20

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

xmen256k

สมมติว่า ผมต้องการสุ่มตัว 5 ตัว โดยมีช่วงตัวเลขที่กำหนดให้

ซึ่งก็ได้มา 1 วิธี ดังนี้ครับ

$questions = range(1,100);
shuffle($questions);

และผลที่ได้ก็คือ มีตัวเลข 1 - 100 อยู่ในอาเรย์

เมื่อจะใช้ 5 ตัว ก็หยิบมาใช้ 5 ตัวแรก

ปัญหาที่ตามมาก็คือ ถ้าผมต้องการสุ่มตัวเลข 1 - ล้าน

มันก็จะมีข้อมูลในอาเรย์ที่ล้านตัวเลข ซึ่งคิดว่ามันน่าจะเปลือง memory พอสมควร

ไม่ทราบว่าพอจะมีวิธีการสุ่มและไม่ซ้ำ แบบอื่นที่ประหยัด memory บ้างไหมครับ

ขอบคุณครับ

Free-Sex

สุ่ม Array 0-9 จำนวน 6 ชุด ชุดละ 5 ครั้ง แล้ว เอามาต่อกันครับ

$questions1 = range(1,10); <--- หลักหน่วย
shuffle($questions);

$questions2 = range(1,10); <--- หลักสิบ
shuffle($questions);

...

หล้กล้าน สุ่มแค่ 0 กับ 1

พอจะได้ไหมครับ

tekub

กำ อ่านโจท ไม่หมด -*-
[direct=http://www.easysoft.co.th/products/minimart]โปรแกรมร้านมินิมาร์ท โปรแกรมขายหน้าร้าน[/direct]
[direct=http://www.easysoft.co.th/products/topup]เติมเงินมือถือออนไลน์ จุดรับชำระบิลออนไลน์[/direct]

max30012540


EleLight-

[direct=http://shop.tag47.com]รีวิวสินค้าออนไลน์[/direct]

xmen256k

อ้างถึงจาก: EleLight- ใน 29 ตุลาคม 2013, 19:36:48
$number = mt_rand(0, 1000000);

มันก็ยังซ้ำอ่าครับ

ผมลองโดย
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);

อ้างถึงจาก: max30012540 ใน 29 ตุลาคม 2013, 19:35:45
สุ่มตัวเลข 5 ตัว rand(10000, 99999)  ???

ยังไงเหรอครับ

max30012540

อ้างถึงจาก: xmen256k ใน 29 ตุลาคม 2013, 20:00:14
อ้างถึงจาก: EleLight- ใน 29 ตุลาคม 2013, 19:36:48
$number = mt_rand(0, 1000000);

มันก็ยังซ้ำอ่าครับ

ผมลองโดย
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);

อ้างถึงจาก: max30012540 ใน 29 ตุลาคม 2013, 19:35:45
สุ่มตัวเลข 5 ตัว rand(10000, 99999)  ???

ยังไงเหรอครับ
rand(เริ่ม, จบ)
สุ่มตัวเลขจำนวน 5 ตัว คือ 5 หลัก และตัวเลข 5 หลัก คือเริ่มที่ 10000 และสุดท้ายที่ 99999 ครับ
$random = rand(10000, 99999);
ค่าในตัวแปร จะเป็นตัวเลขสุ่ม 10000-99999 ครับ

#mt_rand กับ rand ใช้ได้ และใช้แบบเดียวกัน แต่ไปอ่านๆมา mt_rand เหมือนจะสุ่มได้ดีกว่า

Twenty-One

อ่าวอ่านไม่ครบ 1-ล้าน เลยเหรอครับ
# บริการโฮสติ้งขั้นเทพ 24/7 เปิดให้บริการ web hosting มาแล้ว 14 ปี ลูกค้ากว่า 40,000 ราย ให้ความไว้วางใจ
# [direct=https://www.hostneverdie.com]hosting[/direct] คุณภาพสูง ดูแลระบบโดย system engineer ประสบการณ์สูง
# [direct=https://www.hostneverdie.com]host[/direct] เร็ง แรง ไม่มีล่ม ติดตั้ง cms ฟรี
# [direct=https://www.hostneverdie.com/vps-server]vps[/direct] ราคาถูก 50GB 999 บาท

[direct=https://www.hostneverdie.com][/direct]

MapTwoZa


function random($start, $end, $count){
    $result = array();
    while(true){
       $key = rand($start, $end);
       $result[$key] = true;
       if(count($result)==$count) return $result;
    }
}
Good code quality Developer :D

xmen256k

อ้างถึงจาก: max30012540 ใน 29 ตุลาคม 2013, 20:09:05
อ้างถึงจาก: xmen256k ใน 29 ตุลาคม 2013, 20:00:14
อ้างถึงจาก: EleLight- ใน 29 ตุลาคม 2013, 19:36:48
$number = mt_rand(0, 1000000);

มันก็ยังซ้ำอ่าครับ

ผมลองโดย
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);
$number = mt_rand(0, 1000000);

อ้างถึงจาก: max30012540 ใน 29 ตุลาคม 2013, 19:35:45
สุ่มตัวเลข 5 ตัว rand(10000, 99999)  ???

ยังไงเหรอครับ
rand(เริ่ม, จบ)
สุ่มตัวเลขจำนวน 5 ตัว คือ 5 หลัก และตัวเลข 5 หลัก คือเริ่มที่ 10000 และสุดท้ายที่ 99999 ครับ
$random = rand(10000, 99999);
ค่าในตัวแปร จะเป็นตัวเลขสุ่ม 10000-99999 ครับ

#mt_rand กับ rand ใช้ได้ และใช้แบบเดียวกัน แต่ไปอ่านๆมา mt_rand เหมือนจะสุ่มได้ดีกว่า

อ่อครับ แต่มันก็ยังมีซ้ำกันอยู่อ่าครับ

อ้างถึงจาก: Twenty-One ใน 29 ตุลาคม 2013, 20:11:03
อ่าวอ่านไม่ครบ 1-ล้าน เลยเหรอครับ

ใช่ครับ ช่วงมันอาจจะกว้างหน่อยครับ -0-

xmen256k

อ้างถึงจาก: MapTwoZa ใน 29 ตุลาคม 2013, 20:47:31

function random($start, $end, $count){
    $result = array();
    while(true){
       $key = rand($start, $end);
       $result[$key] = true;
       if(count($result)==$count) return $result;
    }
}


ได้แล้วครับ ขอบคุณมากครับ

goldxp

ขออนุญาติเสนอ function อีกรูปแบบหนึ่ง

function fairly_rand($start,$end,$return=1){
//function นี้ $start และ $end ควรจะห่างกันมากๆ
$a = array();
$distance = ($end-$start)/$return;
$a[]=$start;
for($i=$start+$distance;$i<$end-($distance/2);$i+=$distance){
      $a[]=($a[]=floor($i))+1;
}
$a[]=$end;
$results = array();
for($i=0;$i<$return*2;$i+=2){
      $results[] = rand($a[$i],$a[$i+1]);
}
return $results;
}

$numbers = fairly_rand(1,500,5);
var_dump($numbers);

ไม่ซ้ำแน่นอนครับ แต่จะเป็น random ที่ผิดธรรมชาติหน่อย
หารช่วงตัวเลขออกเป็นส่วน เพื่อ random แต่ละส่วนจะได้รับเลือกมาอย่างเท่าเทียมกันทุกครั้ง
[direct=http://web-programming-bookmark.blogspot.com/p/blog-page.html]รับแก้ไขเว็บ[/direct]