ผมมีข้อมูลสองชุด ชุดละ 20 ตัว ต้องการจับคู่แบบสุ่มไม่ซ้ำดังนี้

ชุดที่หนึ่ง (ยึดชุดนี้เป็นหลัก)

A1 ถึง A20

ชุดที่สอง

B1 ถึง B20

มีการจับคู่ประมาณนี้

หาก A1 สุ่มจับคู่ได้ B12 แล้ว

A2 ถึง A20 จะไม่สามารถจับคู่กับ B12 ได้อีกแล้ว

ผมคิดเลขมั่วๆ แบบเบื้องต้น

ชุด A จะจับคู่แบบสุ่มได้ A ละ 5B

งงมั้ยครับ

ภาษาอะไรก็ได้ครับ วิธีคิดก็ได้ครับ
C#,Java,PHP,C,C++

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

phutsa Fri, 30/03/2012 - 23:10

import java.util.ArrayList;
import java.util.LinkedList;
public class randomA1_A20 {

//String a[] = new String[20];
//String b[] = new String[20];
LinkedList<String> a = new LinkedList<String>();
LinkedList<String> b = new LinkedList<String>();
public randomA1_A20() {
	for (int i = 1; i <= 20; i++) {

// a[i-1] = "A"+i;
// b[i-1] = "B"+i;
a.add("A"+i);
b.add("B"+i);
}

	int random;
	ArrayList<String> keepA  = new ArrayList<String>();
	ArrayList<String> keepB  = new ArrayList<String>();
	int j=0;
	while(b.isEmpty()==false) {
		keepA.add(a.get(j));
		random = (int)(Math.random()*b.size())+0;

// System.out.println(random);
keepB.add(b.get(random));
b.remove(random);
j++;
}
// System.out.println(keepB.toString());
for (int i = 0; i < a.size(); i++) {
System.out.println(keepA.get(i)+" = "+keepB.get(i));
}
}
public static void main(String[] args) {
new randomA1_A20();
}
}
/* output ประมาณนี้ครับ ไม่รู้จะใช่ไหม ลองทำเล่น ๆ ดูครับ
A1 = B11
A2 = B15
A3 = B4
A4 = B1
A5 = B9
A6 = B17
A7 = B19
A8 = B16
A9 = B2
A10 = B3
A11 = B6
A12 = B14
A13 = B20
A14 = B7
A15 = B8
A16 = B10
A17 = B13
A18 = B18
A19 = B12
A20 = B5
แนวคิด random ไมซ้ำของจาวานะครับ

  1. การ random ตัวเลข แบบไม่ให้ซ้ำกัน คิดว่าหลายๆคนหากเขียนโปรแกรม random แบบนี้ จะใช้วิธี Loop ซ้อน (ซึ่งก็ใช้ได้อยู่ ) แต่นอกจากวิธีนี้แล้ว เราสามารถใช้ class ArrayList (package java.util) มาช่วยได้ ตัวอย่าง เช่น เราต้องการสร้าง int array ให้เก็บตัวเลข random แตกต่างกัน ตั้งแต่ 0 – 19 หลักการก็คือ
  • สร้าง ArrayList object
  • add data ลงไปใน ArrayList โดย add ตามลำดับ เช่น 0,1, 2, 3, … ตามจำนวน random ที่เราต้องการ ( 20 ตัว )
  • สร้าง loop while ArrayList not empty โดยใน loop นี้ ทำการ random integer ค่าไม่เกิน 20 แล้ว get ค่าจาก ArrayList จาก index ที่ random ได้ นำค่าที่ get มาใส่ลงใน int array ที่สร้างไว้ พร้อมทั้ง remove index นั้นออกจาก ArrayList
  • เท่านี้ก็จะได้ int array ที่เก็บค่า 0-19 ที่แตกต่างกัน
    credit AJ. Prymania
    */

chatcharin Fri, 30/03/2012 - 23:13

class MyData{
public String name;
public MyData relation;
public setName(String name){
this.name = name;
}
public void setRelation(MyData re){
this.relation = re;
re.relation = this;
}
public boolean getStatus(){
return relation==null;
}
public void showRelation(){
System.out.println(this.name+" relation "+relation.name);
}
}

main method

Mydata A[] = new MyData[20];
Mydata B[] = new MyData[20];
for(int i=0;i<20;i++){
A[i].setName("A"+i);
B[i].setName("B"+i);
}
public int getRandom(){
int x = 0;
while(true){
x ++;
int i = (int) ( Math.random()*20)+1;
if(B[i].getStatus())
return i;
if(x > 1000) return 0;
}
}

for(int i=0;i<20;i++){
A[i].setRelation(B[getRandom()]);
A[i].showRelation();
}

คร่าวๆ นะครับ สดๆ เลย

neizod Sat, 31/03/2012 - 11:22

ง่ายๆ เลยก็สลับสมารชิกในตัวแปรชุด B ให้เรียบร้อยซะ แล้วค่อยเอา A มาจับคู่กับ B

{syntaxhighlighter class="brush: python"}
import random

A = list(range(1, 21))
B = list(range(1, 21))

random.shuffle(B)

for a, b in zip(A, B):
print('A = ', a, '& B = ', b)
{/syntaxhighlighter}

การสุ่มแบบไม่ซ้ำนั้น ใช้หลักการง่ายๆก็ได้ เช่นเดียวกับการหยิบส้ม ออกจากกล่อง

หากส้มลูกที่ถูกสุ่มหยิบออกมาแล้ว เราจะไม่มีทางหยิบได้ลูกเดิมแน่นอน เพราะว่าในกล่อง คงไม่มีส้มลูกเดิม ถึง 2 ลูก
(ยกเว้นอีกลูกจะทะลุมาจากมิติคู่ขนาน)

ดังนั้นหากจะให้ array A และ B จับคู่กันโดยไม่ซ้ำ
เราก็สามารถสุ่ม A และ B อย่างละตัว และลบค่านั้นออกจาก array เดิมไป
แค่นี้ เราก็จะไม่มีทางสุ่มได้ค่าเดิมอีก เพราะใน array นั้นไม่มีค่าเดิมอีกต่อไปแล้ว

แนวคิดนี้สามารถประยุกต์ใช้ในแบบอื่นได้เช่น หากเราไม่ต้องการไปแก้ไข Array A และ B เราก็ยังสามารถสร้าง
Array อีกชุดหนึ่ง ซึ่งใช้บอกตำแหน่งที่จับคู่กัน เราก็จะแรนดอมได้เป็น ตำแหน่ง A และ B โดยที่ไม่ต้องแตะต้อง Array A,B เลยด้วยซ้ำ

แต่วิธีของคุณ neizod นี่เจ๋งกว่า เพราะแค่สลับก็พอ
ซึ่งหากว่าไม่อยากไปสลับ Array จริงก็ใช้ Array เลขลำดับแบบที่บอกก็ยังได้

B1 เลือกจับคู่กับชุด A ได้ 20 แบบ
B2 เลือกจับคู่กับชุด A ได้ 19 แบบ
B3 เลือกจับคู่กับชุด A ได้ 18 แบบ
.
.
.
B20 เลือกจับคู่กับชุด A ได้ 1 แบบ

ดังนั้นจำนวนแบบผลลัพธ์สุดท้ายในการสุ่มจำคู่ทั้งหมดที่เป็นไปได้ = 20*19*18*...*1 = 20! แบบ

มีปัญหาอีกแล้วครับ

คือ การแทนที่คำ ปกติเราใช้ str_replace ไม่ก็ preg_replace


$string_replace = array("PHP","PHP","PHP");
$string = "PHP and PHP and PHP";

ต้องการ Output มาเป็น

$string = "PHP and PHP and PHP";

แต่ที่ได้มามันเป็น

$string = "PHP and PHP and PHP";

คือมันแทนที่ทุกคำไปเลยอ่ะครับ

  • and นี่คือคำอื่นๆ นะครับ

เช่น

$string = "การสุ่มแบบไม่ซ้ำนั้น ใช้หลักการง่ายๆก็ได้ เช่นเดียวกับการหยิบส้ม ออกจากกล่อง

หากส้มลูกที่ถูกสุ่มหยิบออกมาแล้ว เราจะไม่มีทางหยิบได้ลูกเดิมแน่นอน เพราะว่าในกล่อง คงไม่มีส้มลูกเดิม ถึง 2 ลูก (ยกเว้นอีกลูกจะทะลุมาจากมิติคู่ขนาน)

ดังนั้นหากจะให้ array A และ B จับคู่กันโดยไม่ซ้ำ เราก็สามารถสุ่ม A และ B อย่างละตัว และลบค่านั้นออกจาก array เดิมไป แค่นี้ เราก็จะไม่มีทางสุ่มได้ค่าเดิมอีก เพราะใน array นั้นไม่มีค่าเดิมอีกต่อไปแล้ว

แนวคิดนี้สามารถประยุกต์ใช้ในแบบอื่นได้เช่น หากเราไม่ต้องการไปแก้ไข Array A และ B เราก็ยังสามารถสร้าง Array อีกชุดหนึ่ง ซึ่งใช้บอกตำแหน่งที่จับคู่กัน เราก็จะแรนดอมได้เป็น ตำแหน่ง A และ B โดยที่ไม่ต้องแตะต้อง Array A,B เลยด้วยซ้ำ

แต่วิธีของคุณ neizod นี่เจ๋งกว่า เพราะแค่สลับก็พอ ซึ่งหากว่าไม่อยากไปสลับ Array จริงก็ใช้ Array เลขลำดับแบบที่บอกก็ยังได้"


$string_replace = array("array ","array ","array ");

หาวิธียังไม่ได้เลย T_T