วันเสาร์ที่ 26 กุมภาพันธ์ พ.ศ. 2554

Java: generating random number in a range


Hello,
I am trying to generate a random number with Java, but random in a specific range. For example, my range is 5-10, meaning that 5 is the smallest possible value the random number can take, and 10 is the biggest. Any other number in between these numbers is possible to be a value, too.
In Java, there is a function random() in the Math class, which returns a double value between 0.0 and 1.0. In the class Random there is a function nextInt(int n), which returns a random value in the range of 0 (inclusive) and n (exclusive). I couldn't find a method, which returns a random value between two numbers.
I have tried the following things, but I still have problems: (minimum and maximum are the smallest and biggest numbers).
Solution 1 :
randomNum = minimum + (int)(Math.random()*maximum);
problem: randomNum takes is assinged values numbers bigger that maximum
Solution 2 :
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum
=  minimum + i;
problem: randomNum takes is assigned values smaller than minimum.
Could you suggest how to solve my problem, or point me to some references? I have tried also browsing through the archive, and found:
but I couldn't solve the problem.
Thank you.


One standard pattern for accomplishing this is:
Min + (int)(Math.random() * ((Max - Min) + 1))
The java Math library function Math.random() generates a double value in the range [0,1). Notice this range does not include the 1.
In order to get a specific range of values first you need to multiply by the magnitude of the range of values you want covered.
Math.random() * ( Max - Min )
This returns a value in the range [0,Max-Min).
For example if you want [5,10] you need cover 5 integer values so you use
Math.random() * 5
This would return a value in the range [0,5)
Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.
Min + (Math.random() * (Max - Min))
You now will get a value in the range [Min,Max). But this is still doesn't include max and you are getting a double value. In order to get the max value included, you need to add 1 to your range parameter (Max - Min) and then truncate the decimal part by casting to an int. This is accomplished via:
Min + (int)(Math.random() * ((Max - Min) + 1))
And there you have it. A random integer value in the range [Min,Max].

special thank

วันเสาร์ที่ 22 มกราคม พ.ศ. 2554

Warning: Calling MEX-file in MATLAB

ปัญหาใหม่ที่เพิ่งเคยเจอ คือพยายามรัน toolbox แล้วระบบแจ้งตามด้านล่าง ตอนแรกก็นึกว่า version 32bit และ 64bit รันร่วมกันไม่ได้หรือเปล่าเพราะว่าอาจจะได้ไฟล์มาคนละรูปแบบ ที่เครื่องมือที่ได้มาก็ไม่ได้บอกว่านำไปใช้กับเวอร์ชันไหน หรือไม่ก็ version ของ matlab เองหรือเปล่าที่อ่านไฟล์ได้คนละแบบ งงไปกันใหม่ 


Warning: Calling MEX-file 'xxxxxxx.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB. 
??? Invalid MEX-file 'xxxxxxx.dll': The specified procedure
could not be found.





หลังจากหาคำตอบอยู่สักพัก ก็เจอว่าลองแก้ปัญหาตามนี้สิ แล้วจะได้ พอดีลองแล้ว ได้ตามที่บอก จึงเขียนเก็บเอาไว้เป็นการแก้ปัญหาในแบบของตัวเองด้วยดีกว่า


คือแก้ไขชื่อไฟล์ในส่วนขยายจาก ".dll" เป็น ".mexw32" ก็สามารถรันรได้ตามปกติแล้วครับ ถ้าเป็น version mac ก็แก้ไขส่วนขยายไฟล์เป็น ".mexmac" 








ขอบคุณ
http://www.dynare.org/phpBB3/viewtopic.php?f=1&t=1986
http://www.mathworks.com/matlabcentral/newsreader/view_thread/239279

วันอังคารที่ 28 ธันวาคม พ.ศ. 2553

Discreate และ Continuous Data

ด้วยความสับสนเกี่ยวกับข้อมูลที่เป็นเป็นข้อมูลต่อเนื่อง หรือไม่ต่อเนื่อง เลยลองหาคำตอบ และได้คำตอบประมาณนี้คือ

ชุดข้อมูลแบบตัวเลขสามารถแบ่งออกได้เป็น 2 กลุ่ม คือ discrete และ continuous

Discrete

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


ตัวอย่าง มีคำถาม 5 ข้อในการสอบวิชาคณิตศาสตร์ ตัวเลือกที่ถูกต้องของคำตามในการตอบคำถามของนักเรียนเป็นข้อมูลแบบไม่ต่อเนื่อง คือ ตัวเลขของคำตอบที่ถูกต้องที่เป็นไปได้ประกอบด้วย : 0, 1, 2, 3, 4, หรือ 5 เป็นค่าที่มีขอบเขต ดังนั้นถ้าต้องการที่วาดตัวเลขลงบนเส้นจำนวน ก็สามารถวาดระยะห่างระหว่างตัวเลขได้อย่างคงที่


ตัวอย่าง ในการสอบใบขับขี่ของ Las Vegas กับจำนวนคนที่สอบผ่านในแต่ละเมือง ถามว่าจำนวนครั้งที่แต่ละคนมาสอบเพื่อให้ผ่านและได้รับใบขับขี่เป้นกี่ครั้ง 1 หรือ 2 หรือ 3 หรือ 4 หรือมากกว่านั้น ดั้งนั้นจำนวนที่เป็นไปได้คือ 1, 2, 3, … ซึ่งเป็นตัวเลขที่ไม่มีขอบเขต แต่เป็นระบบตัวเลขที่มีระยะห่างที่แน่ระหว่างตัวเลข เวลาเขียนลงบนเส้นจำนวนก็สามารถทำได้เช่นกัน ก็ถือว่าเป็นข้อมูลที่ไม่ต่อเนื่องด้วย


ข้อมูลไม่ต่อเนื่อง ปกติแล้วจะมองในค่าของบตัวเลข หรือการนับบางสิ่งที่สามารถใช้ตัวเลขแทนได้


Continuous data

มักจะกล่าวถึงส่วนที่เหลือจากตัวเลขปกติ เช่น ทศนิยม หรือเป็นตัวเลขเกี่ยวกับการประมาณค่าทางด้านกายภาพ


ตัวอย่าง ความสูงของต้นไม้เกี่ยวข้องกับข้อมูลแบบต่อเนื่อง ซึ่งสามารถเป็นไปได้ว่าต้นไม้จะสูงครั้งละ 76.2"  หรือ ประมาณ 76.29 หรือประมาณ 76.2914563782  คุณเชื่อได้หรือไม่ว่ามันจะสูงเท่าไหร่ เป็นไปได้ว่าขึ้นอยู่กับเครื่องมือที่ใช้ในการวัดก็ได้


มีทางเดียวที่จะบอกว่าเป็นข้อมูลแบบต่อเนื่องหรือไม่ ต้องถามตัวเองว่า เป็นไปได้ไหมที่ตัวเลขที่จะเขียนจะต้องติดเศษส่วน หรือทศนิยมด้วย ถ้าใช่ ข้อมูลนั้นจะเป็นข้อมูลแบบต่อเนื่อง


ตัวอย่าง ความยาวของอายุการใช้งานของหลอดไฟ ก็เป็นตัวอย่างหนึ่งของข้อมูลแบบต่อเนื่อง เนื่องจากเป็นได้ว่าอายุการใช้งานของหลอดไฟจะเป็น 800 hours? หรือประมาณ 800.7? หรือประมาณ 800.7354? เพราะบางครั้งอายุของหลอดไฟก็ไม่ได้ 800 hours ตรง ๆ แต่อาจจะมี มากกว่า หรือน้อยกว่าได้ และมากกว่า หรือน้อยกว่าเท่าไหร่ก็ยากต่อการบอกเป็นค่าตัวเลข ก็ถือว่าข้อมูลแบบนี้เป็นข้อมุลแบบต่อเนื่องด้วย