Advertisement
Python253

generate_test_arrays

May 24th, 2024
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: generate_test_arrays.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script generates a file with 10 lines of arrays for testing purposes.
  10.    Each array contains random floating-point numbers.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.  
  15. Functions:
  16.    - generate_random_arrays(num_arrays, array_length):
  17.        Generates a list of arrays with random floating-point numbers.
  18.    - save_arrays_to_file(arrays, filename):
  19.        Saves the generated arrays to a file.
  20.  
  21. Usage:
  22.    - Run the script to generate a file named 'test_arrays.txt' in the current working directory.
  23. """
  24.  
  25. import numpy as np
  26.  
  27. def generate_random_arrays(num_arrays=10, array_length=10):
  28.     """
  29.    Generate a list of arrays with random floating-point numbers.
  30.  
  31.    Parameters:
  32.    num_arrays (int): Number of arrays to generate.
  33.    array_length (int): Length of each array.
  34.  
  35.    Returns:
  36.    list: A list of arrays with random floating-point numbers.
  37.    """
  38.     generated_arrays = []
  39.     for _ in range(num_arrays):
  40.         random_array = np.random.uniform(low=-5.0, high=5.0, size=array_length).tolist()
  41.         generated_arrays.append(random_array)
  42.     return generated_arrays
  43.  
  44. def save_arrays_to_file(arrays, filename='test_arrays.txt'):
  45.     """
  46.    Save the generated arrays to a file.
  47.  
  48.    Parameters:
  49.    arrays (list): A list of arrays to save.
  50.    filename (str): The name of the file to save the arrays to.
  51.    """
  52.     with open(filename, 'w', encoding='utf-8') as file:
  53.         for index, array in enumerate(arrays, start=1):
  54.             array_str = ', '.join(f'{x:.4f}' for x in array)
  55.             file.write(f"{index} - Item {index} - [{array_str}]\n")
  56.  
  57. if __name__ == "__main__":
  58.     # Generate 10 arrays with 10 random floating-point numbers each
  59.     num_arrays_to_generate = 10
  60.     array_length_to_generate = 10
  61.     arrays_to_save = generate_random_arrays(num_arrays_to_generate, array_length_to_generate)
  62.    
  63.     # Save the arrays to 'test_arrays.txt'
  64.     save_arrays_to_file(arrays_to_save)
  65.    
  66.     print(f"Generated {num_arrays_to_generate} arrays and saved to 'test_arrays.txt'")
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement