为什么我与教练相比,我得到了不同形状的图形?

时间:2018-01-29 15:14:54

标签: python graph networkx

我尝试绘制的内容:

  

G = nx.gnp_random_graph(20,0.5)

我用plt.show(G)获得了什么:

my result

我的教练画了什么:用相同的命令:

instructor's result

1 个答案:

答案 0 :(得分:1)

代码如:

G = nx.gnp_random_graph(20,0.5)
G = nx.gnp_random_graph(n=20, p=0.5, seed=None, directed=False)  # equivalent

将使用当前时间或其他资源来初始化PRNG(如seed=None

由于networkx是纯Python和reuses the standardlib,因此归结为python's random module seed

  

random.seed(a =无,版本= 2)

Initialize the random number generator.

If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

If a is an int, it is used directly.

With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used.

With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

Changed in version 3.2: Moved to the version 2 scheme which uses all of the bits in a string seed.

您可以设置种子以使其具有确定性:

G = nx.gnp_random_graph(20,0.5,0)  # or any other int

最后一行将始终输出相同的图形(文档显式调用int!)。

如果您的导师没有修理种子,尝试重现种子有点虚假。