ผู้เขียน หัวข้อ: ขอความรู้ เกี่ยวกับการสุ่ม  (อ่าน 3472 ครั้ง)

0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

ออฟไลน์ arr_012

  • Sr. Member
  • ****
  • กระทู้: 307
  • ค่าของคน อยู่ที่ผลของงาน
  • Respect: 0
    • ดูรายละเอียด
ขอความรู้ เกี่ยวกับการสุ่ม
« เมื่อ: กุมภาพันธ์ 01, 2011, 14:11:01 PM »
0
select *from vn_stat
order by rand(10)

กับ

select *from vn_stat
order by rand()

มันต่างกันตรงไหนครับ
นายอภิชัย  ฉิมวงษ์ขอม (อะ)
โรงพยาบาลมโนรมย์ อ.มโนรมย์ จ.ชัยนาท
Master:HP ProLiant ML150 G6 Intel Xeon E5504 2.00GHz ,12 GB PC3-10600R   FeeBSD 8.2  Percona 5.1.54

ออฟไลน์ Svl2Nuk3

  • Hero Member
  • *****
  • กระทู้: 793
  • Respect: 0
    • ดูรายละเอียด
Re: ขอความรู้ เกี่ยวกับการสุ่ม
« ตอบกลับ #1 เมื่อ: กุมภาพันธ์ 01, 2011, 14:32:38 PM »
0
select *from vn_stat
order by rand(10)

กับ

select *from vn_stat
order by rand()

มันต่างกันตรงไหนครับ

select *from vn_stat
order by rand(10)

เวลารันคำสั่งนี้แต่ละรอบค่าจะเหมือนเดิม


select *from vn_stat
order by rand()

เวลารันคำสั่งนี้แต่ละรอบค่าจะไม่เหมือนกัน


แต่อย่าลืม Limit นะครับ  ลองกด  run  หลาย ๆ รอบดูจะเห็นความแตกต่างครับ

I'm nuke (นุ๊ก)
My Blog  : http://www.codenuke.net
อดีต นวก.คอมฯ รพช.พรหมพิราม => 1 พ.ค. 52 - 30 ก.ย. 54
ปัจจุบัน : Software Engineer บริษัทแห่งหนึ่ง

ออฟไลน์ Svl2Nuk3

  • Hero Member
  • *****
  • กระทู้: 793
  • Respect: 0
    • ดูรายละเอียด
Re: ขอความรู้ เกี่ยวกับการสุ่ม
« ตอบกลับ #2 เมื่อ: กุมภาพันธ์ 01, 2011, 14:40:00 PM »
0
อ่านอันนี้เพิ่มเติมน่าจะเข้าขึ้นนะครับ  (หรือมึนขึ้นกว่าเดิม เพราะผมก็มึน)  ;D ;D ;D

RAND(), RAND(N)

Returns a random floating-point value v in the range 0 <= v < 1.0. If a constant integer argument N is specified, it is used as the seed value, which produces a repeatable sequence of column values. In the following example, note that the sequences of values produced by RAND(3) is the same both places where it occurs.

mysql> CREATE TABLE t (i INT);
Query OK, 0 rows affected (0.42 sec)

mysql> INSERT INTO t VALUES(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT i, RAND() FROM t;
+------+------------------+
| i    | RAND()           |
+------+------------------+
|    1 | 0.61914388706828 |
|    2 | 0.93845168309142 |
|    3 | 0.83482678498591 |
+------+------------------+
3 rows in set (0.00 sec)

mysql> SELECT i, RAND(3) FROM t;
+------+------------------+
| i    | RAND(3)          |
+------+------------------+
|    1 | 0.90576975597606 |
|    2 | 0.37307905813035 |
|    3 | 0.14808605345719 |
+------+------------------+
3 rows in set (0.00 sec)

mysql> SELECT i, RAND() FROM t;
+------+------------------+
| i    | RAND()           |
+------+------------------+
|    1 | 0.35877890638893 |
|    2 | 0.28941420772058 |
|    3 | 0.37073435016976 |
+------+------------------+
3 rows in set (0.00 sec)

mysql> SELECT i, RAND(3) FROM t;
+------+------------------+
| i    | RAND(3)          |
+------+------------------+
|    1 | 0.90576975597606 |
|    2 | 0.37307905813035 |
|    3 | 0.14808605345719 |
+------+------------------+
3 rows in set (0.01 sec)

The effect of using a nonconstant argument is undefined. As of MySQL 5.0.13, nonconstant arguments are not permitted.

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
I'm nuke (นุ๊ก)
My Blog  : http://www.codenuke.net
อดีต นวก.คอมฯ รพช.พรหมพิราม => 1 พ.ค. 52 - 30 ก.ย. 54
ปัจจุบัน : Software Engineer บริษัทแห่งหนึ่ง

ออฟไลน์ arr_012

  • Sr. Member
  • ****
  • กระทู้: 307
  • ค่าของคน อยู่ที่ผลของงาน
  • Respect: 0
    • ดูรายละเอียด
Re: ขอความรู้ เกี่ยวกับการสุ่ม
« ตอบกลับ #3 เมื่อ: กุมภาพันธ์ 01, 2011, 14:44:55 PM »
0
select *from vn_stat
order by rand(10)

กับ

select *from vn_stat
order by rand()

มันต่างกันตรงไหนครับ

select *from vn_stat
order by rand(10)

เวลารันคำสั่งนี้แต่ละรอบค่าจะเหมือนเดิม


select *from vn_stat
order by rand()

เวลารันคำสั่งนี้แต่ละรอบค่าจะไม่เหมือนกัน


แต่อย่าลืม Limit นะครับ  ลองกด  run  หลาย ๆ รอบดูจะเห็นความแตกต่างครับ


แล้วตัวเลขใน rand(?) มันคือค่าอะไรครับ
นายอภิชัย  ฉิมวงษ์ขอม (อะ)
โรงพยาบาลมโนรมย์ อ.มโนรมย์ จ.ชัยนาท
Master:HP ProLiant ML150 G6 Intel Xeon E5504 2.00GHz ,12 GB PC3-10600R   FeeBSD 8.2  Percona 5.1.54

ออฟไลน์ Svl2Nuk3

  • Hero Member
  • *****
  • กระทู้: 793
  • Respect: 0
    • ดูรายละเอียด
Re: ขอความรู้ เกี่ยวกับการสุ่ม
« ตอบกลับ #4 เมื่อ: กุมภาพันธ์ 01, 2011, 14:50:31 PM »
0
rand(N)

If a constant integer argument N is specified, it is used as the seed value, which produces a repeatable sequence of column values.

มันคือค่าที่ใส่เข้าไปแล้วจะทำให้ลำดับของคอลัมเหมือนเดิมในการ query แต่ละครั้ง
แต่ค่าที่ return  ออกมาจากฟังก์ชัน  rand(N)  ไม่รู้ว่าคำนวณยังไงครับ   ;D ;D
I'm nuke (นุ๊ก)
My Blog  : http://www.codenuke.net
อดีต นวก.คอมฯ รพช.พรหมพิราม => 1 พ.ค. 52 - 30 ก.ย. 54
ปัจจุบัน : Software Engineer บริษัทแห่งหนึ่ง