Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/full/demo_random_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Script to demo the random integer generator used by the random graph generators.
"""

#!/usr/bin/env python

from planarity import (
gp_GetRandomNumber,
)

if __name__ == "__main__":

print("Some pseudo-random numbers between 1 and 10:")
print(gp_GetRandomNumber(1, 10))
print(gp_GetRandomNumber(1, 10))
print(gp_GetRandomNumber(1, 10))
print(gp_GetRandomNumber(1, 10))
9 changes: 9 additions & 0 deletions planarity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .full.graphLib import (
gp_GetQuietMode,
gp_SetQuietMode,
gp_GetRandomNumber,
)

# Surfaced from planarity/c/graphLib/graphLib.h
Expand Down Expand Up @@ -107,6 +108,14 @@
from .full.graph import Graph
from .full.g6IterationUtils import G6ReadIterator, G6WriteIterator

# Seed the random number generator in the C library so that the
# random number and random graph generators will produce different
# outputs on each run of a script.
from .full.graphLib import (
seed_rng,
)
seed_rng()

# NOTE: In the future, we could automatically generate the version number by
# configuring setuptools-scm, but presently this seems simpler.
__version__ = "1.0.1"
2 changes: 2 additions & 0 deletions planarity/full/cgraphLib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ cdef extern from "../c/graphLib/lowLevelUtils/apiutils.h":
int gp_GetQuietMode()
void gp_SetQuietMode(int newQuietMode)

int gp_GetRandomNumber(int NMin, int NMax)


cdef extern from "../c/graphLib/graphLib.h":
const char *gp_GetProjectVersionFull()
Expand Down
26 changes: 26 additions & 0 deletions planarity/full/graphLib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ of the caller to make sure these functions are being called legitimately.
from planarity.full cimport cgraphLib
from planarity.full.cgraphLib cimport graphP, G6ReadIteratorP, G6WriteIteratorP

from libc.stdlib cimport srand

# Surfaced from planarityc/graphLib/lowLevelUtils/apiutils.h
QUIETMODE_NONE = cgraphLib.QUIETMODE_NONE
Expand Down Expand Up @@ -144,6 +145,31 @@ def gp_SetQuietMode(int newQuietMode) -> None:
cgraphLib.gp_SetQuietMode(newQuietMode)


def gp_GetRandomNumber(int NMin, int NMax) -> int:
"""Generates a pseudo-random integer number in the range of the parameters.

Like ``random.randint()`` except this method calls the same C-layer
``gp_GetRandomNumber()`` method that is called by the random graph
generator methods such as ``gp_CreateRandomGraph()``.

Args:
NMin: The lower bound
NMax: The upper bound

Returns:
A pseudo-random integer in the closed range [NMin, NMax]
"""
return cgraphLib.gp_GetRandomNumber(NMin, NMax)

def seed_rng() -> None:
# Called by __init__.py to seed the pseudo-random number generator
# used by gp_GetRandomNumber() and the random graph generator methods.
# We seed with a random int generated by Python because Python seeds
# its random number generator with a true source of entropy when
# available and only falls back to system time otherwise.
import random
srand(random.randint(0, 4294967295))

def gp_GetProjectVersionFull() -> str:
"""Returns the Edge Addition Planarity Suite's project release version.

Expand Down
Loading