class_docstr: Defines the conditional probability distribution table (CPD table)
  
  Parameters
  ----------
  variable: int, string (any hashable python object)
      The variable whose CPD is defined.
  
  variable_card: integer
      Cardinality/no. of states of `variable`
  
  values: 2D array, 2D list or 2D tuple
      Values for the CPD table. Please refer the example for the
      exact format needed.
  
  evidence: array-like
      List of variables in evidences(if any) w.r.t. which CPD is defined.
  
  evidence_card: array-like
      cardinality/no. of states of variables in `evidence`(if any)
  
  Examples
  --------
  For a distribution of P(grade|diff, intel)
  
  +---------+-------------------------+------------------------+
  |diff:    |          easy           |         hard           |
  +---------+------+--------+---------+------+--------+--------+
  |aptitude:| low  | medium |  high   | low  | medium |  high  |
  +---------+------+--------+---------+------+--------+--------+
  |gradeA   | 0.1  | 0.1    |   0.1   |  0.1 |  0.1   |   0.1  |
  +---------+------+--------+---------+------+--------+--------+
  |gradeB   | 0.1  | 0.1    |   0.1   |  0.1 |  0.1   |   0.1  |
  +---------+------+--------+---------+------+--------+--------+
  |gradeC   | 0.8  | 0.8    |   0.8   |  0.8 |  0.8   |   0.8  |
  +---------+------+--------+---------+------+--------+--------+
  
  values should be
  [[0.1,0.1,0.1,0.1,0.1,0.1],
  [0.1,0.1,0.1,0.1,0.1,0.1],
  [0.8,0.8,0.8,0.8,0.8,0.8]]
  
  >>> cpd = TabularCPD('grade',3,[[0.1,0.1,0.1,0.1,0.1,0.1],
  ...                             [0.1,0.1,0.1,0.1,0.1,0.1],
  ...                             [0.8,0.8,0.8,0.8,0.8,0.8]],
  ...                             evidence=['diff', 'intel'], evidence_card=[2,3])
  >>> print(cpd)
  +---------+---------+---------+---------+---------+---------+---------+
  | diff    | diff_0  | diff_0  | diff_0  | diff_1  | diff_1  | diff_1  |
  +---------+---------+---------+---------+---------+---------+---------+
  | intel   | intel_0 | intel_1 | intel_2 | intel_0 | intel_1 | intel_2 |
  +---------+---------+---------+---------+---------+---------+---------+
  | grade_0 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
  +---------+---------+---------+---------+---------+---------+---------+
  | grade_1 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
  +---------+---------+---------+---------+---------+---------+---------+
  | grade_2 | 0.8     | 0.8     | 0.8     | 0.8     | 0.8     | 0.8     |
  +---------+---------+---------+---------+---------+---------+---------+
  >>> cpd.values
  array([[[ 0.1,  0.1,  0.1],
          [ 0.1,  0.1,  0.1]],
         [[ 0.1,  0.1,  0.1],
          [ 0.1,  0.1,  0.1]],
         [[ 0.8,  0.8,  0.8],
          [ 0.8,  0.8,  0.8]]])
  >>> cpd.variables
  ['grade', 'diff', 'intel']
  >>> cpd.cardinality
  array([3, 2, 3])
  >>> cpd.variable
  'grade'
  >>> cpd.variable_card
  3
class_docstr_tok: ["Defines","the","conditional","probability","distribution","table","(","CPD","table",")","Parameters","--","--","--","--","--","variable",":","int",",","string","(","any","hashable","python","object",")","The","variable","whose","CPD","is","defined",".","variable_card",":","integer","Cardinality\/no",".","of","states","of","`","variable","`","values",":","2D","array",",","2D","list","or","2D","tuple","Values","for","the","CPD","table",".","Please","refer","the","example","for","the","exact","format","needed",".","evidence",":","array-like","List","of","variables","in","evidences","(","if","any",")","w.r.t",".","which","CPD","is","defined",".","evidence_card",":","array-like","cardinality\/no",".","of","states","of","variables","in","`","evidence","`","(","if","any",")","Examples","--","--","--","--","For","a","distribution","of","P","(","grade|diff",",","intel",")","+","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","+","|diff",":","|","easy","|","hard","|","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","+","|aptitude",":","|","low","|","medium","|","high","|","low","|","medium","|","high","|","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","+","|gradeA","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","+","|gradeB","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","+","|gradeC","|","0.8","|","0.8","|","0.8","|","0.8","|","0.8","|","0.8","|","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","-+","--","--","--","+","--","--","--","--","+","--","--","--","--","+","values","should","be","[","[","0.1,0.1,0.1,0.1,0.1,0.1","]",",","[","0.1,0.1,0.1,0.1,0.1,0.1","]",",","[","0.8,0.8,0.8,0.8,0.8,0.8","]","]",">",">",">","cpd","=","TabularCPD","(","'grade',3",",","[","[","0.1,0.1,0.1,0.1,0.1,0.1","]",",","...","[","0.1,0.1,0.1,0.1,0.1,0.1","]",",","...","[","0.8,0.8,0.8,0.8,0.8,0.8","]","]",",","...","evidence=","[","'diff","'",",","'intel","'","]",",","evidence_card=","[","2,3","]",")",">",">",">","print","(","cpd",")","+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","|","diff","|","diff_0","|","diff_0","|","diff_0","|","diff_1","|","diff_1","|","diff_1","|","+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","|","intel","|","intel_0","|","intel_1","|","intel_2","|","intel_0","|","intel_1","|","intel_2","|","+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","|","grade_0","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","|","grade_1","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","0.1","|","+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","|","grade_2","|","0.8","|","0.8","|","0.8","|","0.8","|","0.8","|","0.8","|","+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+","--","--","--","--","-+",">",">",">","cpd.values","array","(","[","[","[","0.1",",","0.1",",","0.1","]",",","[","0.1",",","0.1",",","0.1","]","]",",","[","[","0.1",",","0.1",",","0.1","]",",","[","0.1",",","0.1",",","0.1","]","]",",","[","[","0.8",",","0.8",",","0.8","]",",","[","0.8",",","0.8",",","0.8","]","]","]",")",">",">",">","cpd.variables","[","'grade","'",",","'diff","'",",","'intel","'","]",">",">",">","cpd.cardinality","array","(","[","3",",","2",",","3","]",")",">",">",">","cpd.variable","'grade'",">",">",">","cpd.variable_card","3"]
docstr: Generates a TabularCPD instance with random values on `variable` with
  parents/evidence `evidence` with cardinality/number of states as given
  in `cardinality`.
  
  Parameters
  ----------
  variable: str, int or any hashable python object.
      The variable on which to define the TabularCPD.
  
  evidence: list, array-like
      A list of variable names which are the parents/evidence of `variable`.
  
  cardinality: dict (default: None)
      A dict of the form {var_name: card} specifying the number of states/
      cardinality of each of the variables. If None, assigns each variable
      2 states.
  
  state_names: dict (default: {})
      A dict of the form {var_name: list of states} to specify the state names
      for the variables in the CPD. If state_names=None, integral state names
      starting from 0 is assigned.
  
  Returns
  -------
  Random CPD: pgmpy.factors.discrete.TabularCPD
      A TabularCPD object on `variable` with `evidence` as evidence with random values.
  
  Examples
  --------
  >>> from pgmpy.factors.discrete import TabularCPD
  >>> TabularCPD(variable='A', evidence=['C', 'D'],
  ...            cardinality={'A': 3, 'B': 2, 'C': 4})
  <TabularCPD representing P(A:3 | C:4, B:2) at 0x7f95e22b8040>
  >>> TabularCPD(variable='A', evidence=['C', 'D'],
  ...            cardinality={'A': 2, 'B': 2, 'C': 2},
  ...            state_names={'A': ['a1', 'a2'],
  ...                         'B': ['b1', 'b2'],
  ...                         'C': ['c1', 'c2']})
docstr_tok: ["Generates","a","TabularCPD","instance","with","random","values","on","`","variable","`","with","parents\/evidence","`","evidence","`","with","cardinality\/number","of","states","as","given","in","`","cardinality","`",".","Parameters","--","--","--","--","--","variable",":","str",",","int","or","any","hashable","python","object",".","The","variable","on","which","to","define","the","TabularCPD",".","evidence",":","list",",","array-like","A","list","of","variable","names","which","are","the","parents\/evidence","of","`","variable","`",".","cardinality",":","dict","(","default",":","None",")","A","dict","of","the","form","{","var_name",":","card","}","specifying","the","number","of","states\/","cardinality","of","each","of","the","variables",".","If","None",",","assigns","each","variable","2","states",".","state_names",":","dict","(","default",":","{","}",")","A","dict","of","the","form","{","var_name",":","list","of","states","}","to","specify","the","state","names","for","the","variables","in","the","CPD",".","If","state_names=None",",","integral","state","names","starting","from","0","is","assigned",".","Returns","--","--","--","-","Random","CPD",":","pgmpy.factors.discrete.TabularCPD","A","TabularCPD","object","on","`","variable","`","with","`","evidence","`","as","evidence","with","random","values",".","Examples","--","--","--","--",">",">",">","from","pgmpy.factors.discrete","import","TabularCPD",">",">",">","TabularCPD","(","variable=","'","A","'",",","evidence=","[","'","C","'",",","'D","'","]",",","...","cardinality=","{","'","A","'",":","3",",","'","B","'",":","2",",","'","C","'",":","4","}",")","<","TabularCPD","representing","P","(","A:3","|","C:4",",","B:2",")","at","0x7f95e22b8040",">",">",">",">","TabularCPD","(","variable=","'","A","'",",","evidence=","[","'","C","'",",","'D","'","]",",","...","cardinality=","{","'","A","'",":","2",",","'","B","'",":","2",",","'","C","'",":","2","}",",","...","state_names=","{","'","A","'",":","[","'a1","'",",","'a2","'","]",",","...","'","B","'",":","[","'b1","'",",","'b2","'","]",",","...","'","C","'",":","[","'c1","'",",","'c2","'","]","}",")"]
code: def get_random(
      variable, evidence=None, cardinality=None, state_names={}, seed=42
  ):
      """
      Generates a TabularCPD instance with random values on `variable` with
      parents/evidence `evidence` with cardinality/number of states as given
      in `cardinality`.
  
      Parameters
      ----------
      variable: str, int or any hashable python object.
          The variable on which to define the TabularCPD.
  
      evidence: list, array-like
          A list of variable names which are the parents/evidence of `variable`.
  
      cardinality: dict (default: None)
          A dict of the form {var_name: card} specifying the number of states/
          cardinality of each of the variables. If None, assigns each variable
          2 states.
  
      state_names: dict (default: {})
          A dict of the form {var_name: list of states} to specify the state names
          for the variables in the CPD. If state_names=None, integral state names
          starting from 0 is assigned.
  
      Returns
      -------
      Random CPD: pgmpy.factors.discrete.TabularCPD
          A TabularCPD object on `variable` with `evidence` as evidence with random values.
  
      Examples
      --------
      >>> from pgmpy.factors.discrete import TabularCPD
      >>> TabularCPD(variable='A', evidence=['C', 'D'],
      ...            cardinality={'A': 3, 'B': 2, 'C': 4})
      <TabularCPD representing P(A:3 | C:4, B:2) at 0x7f95e22b8040>
      >>> TabularCPD(variable='A', evidence=['C', 'D'],
      ...            cardinality={'A': 2, 'B': 2, 'C': 2},
      ...            state_names={'A': ['a1', 'a2'],
      ...                         'B': ['b1', 'b2'],
      ...                         'C': ['c1', 'c2']})
      """
      generator = np.random.default_rng(seed=seed)
  
      if evidence is None:
          evidence = []
  
      if cardinality is None:
          cardinality = {var: 2 for var in chain([variable], evidence)}
      else:
          for var in chain([variable], evidence):
              if var not in cardinality.keys():
                  raise ValueError(
                      f"Cardinality for variable: {var} not specified."
                  )
  
      if len(evidence) == 0:
          values = generator.random((cardinality[variable], 1))
          values = values / np.sum(values, axis=0)
          node_cpd = TabularCPD(
              variable=variable,
              variable_card=cardinality[variable],
              values=values,
              state_names=state_names,
          )
      else:
          parent_card = [cardinality[var] for var in evidence]
          values = generator.random(
              (cardinality[variable], np.prod(parent_card))
          )
          values = values / np.sum(values, axis=0)
          node_cpd = TabularCPD(
              variable=variable,
              variable_card=cardinality[variable],
              values=values,
              evidence=evidence,
              evidence_card=parent_card,
              state_names=state_names,
          )
  
      return node_cpd
  
imports: import csv
  import numbers
  from itertools import chain, product
  from shutil import get_terminal_size
  from warnings import warn
  import numpy
  import torch
  from pgmpy import config
  from pgmpy.extern import tabulate
  from pgmpy.factors.discrete import DiscreteFactor
  from pgmpy.utils import compat_fns
label_desc: Use image node_id 16 for calling the TabularCPD obj's underlying member method code with example usage: obj.get_random(variable, evidence, cardinality, state_names, seed) and returns: node_cpd