Tags:

ผมคิดไม่ออกซักทีอะครับ รบกวนช่วยชี้แนะทีครับ

Get latest news from Blognone
By: btssky
ContributoriPhoneAndroidRed Hat
on 29 June 2012 - 18:08 #438985

เก็บเลขที่หยิบมาแล้วไว้ใน array ทุกครั้งที่หยิบเอามาเปรียบเทียบกับค่าที่เก็บไว้ถ้าเหมือนซ้ำให้หยิบใหม่ ของซ้ำไม่ต้องแสดงผลออกมา

หรือ

อันไหนที่หยิบออกแล้วให้ unset ค่านั้นออกจาก array 1-10

วิธีที่สองเร็วกว่า

By: incredibles
iPhoneWindows PhoneAndroidUbuntu
on 29 June 2012 - 18:10 #438986

สุ่ม 1-10 เก็บไว้ใน array ก่อน
แล้วตัวต่อไปให้สุ่มมาใส่ตัวแปรไว้ก่อน ตัวแปรอะไรก็ได้
แล้วเอาตัวแปรนั้นไปตรวจกับในarray ว่าซ้ำหรือเปล่า
ถ้าซ้ำวนลูปทำใหม่ ถ้าไม่ซ้ำ เอาค่าไปใส่ array ตัวถัดไป แล้ววนลูป
ทำจนครบ array

By: varavut
ContributorWindows PhoneAndroidBlackberry
on 29 June 2012 - 22:01 #438988

สุ่มเลขมาใส่ในเซ็ตจนครบ 5 ตัวครับ

By: nanana
Windows PhoneAndroidUbuntu
on 29 June 2012 - 18:50 #439016
  1. เก็บค่าไว้ใน Array 10 ตัว

  2. random แบบกำหนดให้ไม่เกิน 10 ได้เลขอะไรมา
    เอาไปวนลูปใน array ตกตำแหน่งไหนเอาเลขนั้นออกมา
    แล้วเอาค่านั้นออกจาก array ด้วย

  3. ทำซ้ำข้อ 2 อีก 4 ครั้ง

ไม่รู้จะพอช่วยได้หรือเปล่าหนอ?

By: narok119
ContributoriPhone
on 29 June 2012 - 18:58 #439024

int array = {1,2,....10}

for(int i = 1 ; i <= 5 ; i++)

{

int randomIndex = random(0,array.size);

print("Random number "+i+" : "+array[randomIndex]);

array.remove(randomIndex);

}

By: JavaDevil
iPhoneUbuntuIn Love
on 29 June 2012 - 20:44 #439083 Reply to:439024

import random
i = range(1,10)
random.shuffle(i)
for j in i[0:5]:
print j

By: aratnon on 29 June 2012 - 19:43 #439055

ได้ละครับ ขอบคุณมากครับ
แล้วไม่ทราบว่า อาร์เรย์ใน c เราสามารถเอาสมาชิกในอาร์เรย์ออกแล้วปรับค่า index ได้ไหมอะครับ

By: mr_tawan
ContributoriPhoneAndroidWindows
on 29 June 2012 - 22:52 #439137 Reply to:439055
mr_tawan's picture

ไม่ได้ครับ


  • 9tawan.net บล็อกส่วนตัวฮับ
By: mr_tawan
ContributoriPhoneAndroidWindows
on 29 June 2012 - 23:00 #439140
mr_tawan's picture

เอามาจากอัลกอของคนแถวนี้สักคน จำไม่ได้ว่าใคร :P

{syntaxhighlighter class="c"}

srand(time(0));

bool taken[10];

int i = 0;
for(i = 0; i<10; i++)
{
taken[i] = false;
}

int takenCount = 5;

while(takenCount > 0)
{
int random = (rand()*100) % 10;
if(taken[random] == true)
continue;

taken[random] = true;
printf("%d\n", random+1);
takenCount--;
}
{/syntaxhighlighter}


  • 9tawan.net บล็อกส่วนตัวฮับ
By: Invisible Force
ContributoriPhoneAndroidUbuntu
on 30 June 2012 - 11:53 #439377 Reply to:439140
Invisible Force's picture

ใน while loop ใช้ random ไปเรื่อยๆ ถ้าเจอซ้ำให้ random ใหม่ .. แบบนี้มันไม่ค่อยจ๊าบนะครับ ^^

เดี๋ยวต้องให้เหตุผลว่าทำไมผมคิดว่ายังไม่จ๊าบ .. เพราะว่าการ random ใหม่จากค่า max เดิมและตัวที่เลือกไปแล้วยังเกี่ยวข้องอยู่ อาจจะเกิดการ random แบบไม่แฟร์ขึ้นได้ครับ อิอิ

By: put4558350
ContributorAndroidUbuntuWindows
on 30 June 2012 - 12:54 #439500 Reply to:439377
put4558350's picture

ของคุณ narok119 ไช้ cpu น้อย เหมาะสำหรับการสุ่มเลขหลายตัว ที่ค่าสูงสุดไม่มาก

ส่วนข้างบนนี้ ไช้ ram น้อย เหมาะสำหรับการสุ่มเลขไม่กี่ตัว ที่มีค่าสูงสุดมากๆครับ การสุ่มแบบนี้ถือว่าแฟร์นะครับ


samsung ใหญ่แค่ใหน ?
https://youtu.be/6Afpey7Eldo

By: Zatang
ContributoriPhoneAndroid
on 2 July 2012 - 09:23 #440001 Reply to:439377

จริงครับ ผิดหลักสถิติ แต่ถ้าไม่ได้ใช้งานอะไรเชิงวิชาการที่เคี่ยวๆ ก็คงไม่มีปัญหา


อคติทำให้คนรับเหตุผลด้านเดียว

By: mementototem
ContributorJusci&#039;s WriterAndroidWindows
on 30 June 2012 - 15:55 #439564
mementototem's picture

ใน python มีฟังชั่นให้

{syntaxhighlighter class="brush: python"}
import random
sam = range(1, 10)
k = 7
result = random.sample(sam, k)
{/syntaxhighlighter}


Jusci - Google Plus - Twitter

By: saknarak
Android
on 30 June 2012 - 16:58 #439584
saknarak's picture

ทำเหมือนสับไพ่ครับ
แล้วหยิบไพ่ 5 ใบแรก แค่นี้ก็ไม่ซ้ำแล้วครับ


for (int i = 0; i < 100; i++) { // ทำกี่รอบก็ได้ตามใจ
  i1 = Math.random() % 10;
  i2 = Math.random() % 10;
  if (i1 != i2)
    continue;
  swap(data, i1, i2); // สลับค่าข้อมูลตำแหน่ง i1 และ i2
}
for(int i = 0; i < 5; i++) {
  print data[i];
}
By: narok119
ContributoriPhone
on 1 July 2012 - 04:32 #439700 Reply to:439584

โอ้ว ไม่เคยนึกถึงวิธีนี้เลย

By: may3rd
iPhoneWindows PhoneAndroidUbuntu
on 2 July 2012 - 09:34 #440005 Reply to:439700

ระหว่างอ่านความเห็นอื่นๆ คิดวิธีนี้เหมือนกัน แต่ว่าวิธีเช็คว่าหยิบเลขนั้นไปแล้วน่าจะเร็วกว่า

By: Invisible Force
ContributoriPhoneAndroidUbuntu
on 2 July 2012 - 12:06 #440077 Reply to:439584
Invisible Force's picture

อย่าให้ต้องคิด Big-O นะ .. อิอิ

(Big-O ใช้สำหรับ การทำวิเคราะห์ประสิทธิภาพของวิธีการ)

By: tearfire on 30 June 2012 - 20:49 #439625
tearfire's picture

pop ข้อมูลที่เลือก
กับ
random ด้วยเวลากับวันที่ครับ ;)

By: lew
FounderJusci&#039;s WriterMEconomicsAndroid
on 6 October 2017 - 19:08 #1011951
lew's picture

ไพธอน

random.sample(range(1,11),5)


lewcpe.com, @wasonliw