Jiangsheng的CSDN Digest (Dec 2005)[10]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

From http://en.wikipedia.org/wiki/Shuffle:

In computer science, shuffling is equivalent to generating a random permutation of the cards. There are two basic algorithms for doing this, both popularized by Donald Knuth. The first is simply to assign a random number to each card, and then to sort the cards in order of their random numbers. This will generate a random permutation, unless two of the random numbers generated are the same. This can be eliminated either by retrying these cases, or reduced to an arbitrarily low probability by choosing a sufficiently wide range of random number choices.

The second, generally known as the Knuth shuffle or Fisher-Yates shuffle[1], is a linear-time algorithm (as opposed to the previous O(n log n) algorithm if using efficient sorting such as mergesort or heapsort), which involves moving through the pack from top to bottom, swapping each card in turn with another card from a random position in the part of the pack that has not yet been passed through (including itself). Providing that the random numbers are unbiased, this will always generate a random permutation.

Notice that great care needs to be taken in implementing the Knuth shuffle; even slight deviations from the correct algorithm will produce biased shuffles. For example, working your way through the pack swapping each card in turn with a random card from any part of the pack is an algorithm with nn different possible execution paths, yet there are only n! permutations. A counting argument based on the pigeonhole principle will clearly show that this algorithm cannot produce an unbiased shuffle, unlike the true Knuth shuffle, which has n! execution paths which match up one-to-one with the possible permutations.

Whichever algorithm is chosen, it is important that a source of truly random numbers is used as the input to the shuffling algorithm. If a biased or pseudo-random source of random numbers is used, the output shuffles may be non-random in a way that is hard to detect, but easy to exploit by someone who knows the characteristics of the "random" number source.

References
D. Aldous and P. Diaconis, "Shuffling cards and stopping times", American Mathematical Monthly 93 (1986), 333–348
Trefethen, L. N. and Trefethen, L. M. "How many shuffles to randomize a deck of cards?" Proceedings of the Royal Society London A 456, 2561–2568 (2000)

http://www.math.washington.edu/~chartier/Shuffle/
http://www2.toki.or.id/book/AlgDesignManual/BOOK/BOOK4/NODE151.HTM


窗口句柄失效时抛出的异常是什么?如何捕获?如何获取当前IE窗口的URL(VC/MFC 基础类 )


我在MFC程序里检查各个IE窗口的URL并进行相应的处理。可是有时候用户关闭某个窗口程序就会出现异常
(因为这时候窗口句柄已经失效了)。如何捕捉该异常呢?另外能否直接获取当前激活的IE窗口的URL?
相关代码:

SHDocVw::IShellWindowsPtr m_spSHWinds;
if (m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK)
{
CoUninitialize();
return EmptyString;
}
int n = m_spSHWinds->GetCount();
for (int i = 0; i < n; i++)
{
//....
}


http://blog.joycode.com/jiangsheng/archive/2005/10/20/65489.aspx


VC2003中新建了一个MFC的程序,如何加入一个ACTIVEX控件(.NET技术 VC.NET )


http://msdn.microsoft.com/library/en-us/vccore/html/vcgrfWhereIsClassWizardInVisualCNET.asp


在htm中接受com控件发出的事件(VC/MFC ATL/ActiveX/COM )


我为客户做了一个com控件有一些事件(如OnStateChange)发出,客户要求用htm调用。
一开始,一切正常htm中调用代码如下:

<OBJECT ID="DvdPlayCtl" CLASSID="CLSID:EE9626A3-976C-470C-8282-07AB2FE2F85F"></OBJECT>

<SCRIPT language="JavaScript">

DvdPlayCtl.attachEvent("OnStateChange", MyOnStateChange);

function MyOnStateChange(state,info)
{
alert("state change to "+state+" ,"+info);
}
</script>
则一旦com的状态发生改变就发出OnStateChange事件,htm就可以正常接受并提示,但后来客户要求用
另一种方式声明com控件,代码如下:

<SCRIPT language="JavaScript">
var DvdPlayCtl = new ActiveXObject("DvdPlayCtl.DvdPlayCtl");
</script>
即动态生成此com控件,则运行htm时以前的代码DvdPlayCtl.attachEvent部分出错:“对象不支持此操作”

本文关键:Jiangsheng的CSDN Digest (Dec 2005)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top