code: def test_better_than_random_policy(
      policy: bandit.base.Policy, env: gym.Env
  ):
      """Test that the policy is better than random."""
  
      n_trials = 30
      n_successes = 0
  
      for _ in range(n_trials):
          policy = policy.clone()
          random_policy = RandomPolicy()
  
          env_seed = random.randint(0, 2**32 - 1)
          env = copy.deepcopy(env)
          _ = env.reset(seed=env_seed)
          _ = env.action_space.seed(env_seed)
  
          random_env = copy.deepcopy(env)
          _ = random_env.reset(seed=env_seed)
          _ = random_env.action_space.seed(env_seed)
  
          policy_reward = 0.0
          random_reward = 0.0
  
          terminated, truncated = False, False
  
          arm_ids = list(range(env.action_space.n))  # type: ignore
  
          while not terminated and not truncated:
              arm_id = policy.pull(arm_ids)  # type: ignore
              (
                  observation,
                  reward,
                  terminated,
                  truncated,
                  info,
              ) = env.step(arm_id)
              policy.update(arm_id, reward)
              policy_reward += reward
  
              random_arm_id = random_policy.pull(arm_ids)  # type: ignore
              (
                  observation,
                  reward,
                  terminated,
                  truncated,
                  info,
              ) = random_env.step(random_arm_id)
              random_policy.update(random_arm_id, reward)
              random_reward += reward
  
          n_successes += policy_reward > random_reward
  
      assert n_successes > n_trials * 0.8
  
file_path: river/river/bandit/test_policies.py
imports: from __future__ import annotations
  import copy
  import importlib
  import inspect
  import random
  import gym
  import pytest
  from river import bandit, metrics
  from .test_envs import _iter_envs
label_desc: Use image node_id 3 for calling a global function with example usage: test_better_than_random_policy(policy, env) without return types