From a45016c0eade904c2e32faa7b33fe867ea6e2257 Mon Sep 17 00:00:00 2001 From: john-boyer-phd Date: Fri, 25 Sep 2026 10:43:33 -0700 Subject: [PATCH] Surface gp_GetRandomNumber and seed its random number generator --- examples/full/demo_random_int.py | 16 ++++++++++++++++ planarity/__init__.py | 9 +++++++++ planarity/full/cgraphLib.pxd | 2 ++ planarity/full/graphLib.pyx | 26 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 examples/full/demo_random_int.py diff --git a/examples/full/demo_random_int.py b/examples/full/demo_random_int.py new file mode 100644 index 0000000..3b24036 --- /dev/null +++ b/examples/full/demo_random_int.py @@ -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)) diff --git a/planarity/__init__.py b/planarity/__init__.py index a1cf81a..69e2d5a 100644 --- a/planarity/__init__.py +++ b/planarity/__init__.py @@ -12,6 +12,7 @@ from .full.graphLib import ( gp_GetQuietMode, gp_SetQuietMode, + gp_GetRandomNumber, ) # Surfaced from planarity/c/graphLib/graphLib.h @@ -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" diff --git a/planarity/full/cgraphLib.pxd b/planarity/full/cgraphLib.pxd index a7561ac..106ffe4 100644 --- a/planarity/full/cgraphLib.pxd +++ b/planarity/full/cgraphLib.pxd @@ -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() diff --git a/planarity/full/graphLib.pyx b/planarity/full/graphLib.pyx index c6c8b51..dafdce3 100644 --- a/planarity/full/graphLib.pyx +++ b/planarity/full/graphLib.pyx @@ -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 @@ -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.