Skip to content

Enforcing determinism when using PRNGs? #8

@sdmccabe

Description

@sdmccabe

It's not a big deal for now but ultimately we'll want to be able to set seeds for the rewiring algorithms. I see a few possibilities for this:

  1. Use the standard library's random, and set the seed using random.seed($SEED). I don't like this because it mutates global state.
  2. Use numpy's Generator object. This is probably preferable but a bit heavy (and tricky for users).
  3. Add a parameter for passing a random seed at initialization, store a random.Random as an attribute, and call that to generate random numbers.
  4. Do the same, with a np.random.Generator.

I think #3 could look like the following:

class BaseRewirer:
	<...>
	def __init__(seed: Optional[int] = None):
		self.rand = random.Random(seed)
		
	<...>
	def rewire(self, G, **kwargs):
		# selecting a random edge
		rand_edge = rand.choice(list(G.edges))
	<...>

The challenge with using np.random.Generator is that numpy randomness wants to return arrays, which we don't always want (e.g., when sampling edges).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions