Git Product home page Git Product logo

stdrand.h's Introduction

stdrand.h C Library

License: MIT GitHub Follow Facebook

stdrand.h is a simple C library that provides functions for generating random integers and making random choices from arrays of characters or integers.

Table of Contents

  • Functions
    • long rand_int(const long min, const long max) (Details)
    • char rand_choice_char(const char* arr) (Details)
    • long rand_choice_int(const int* arr, const int s) (Details)
    • char rand_char(const char a, const char b) (Details)

Functions

long rand_int(const long min, const long max)

Generates a random integer within the specified range [min, max] (inclusive).

  • Parameters:

    • min: The minimum value of the range.
    • max: The maximum value of the range.
  • Returns:

    • A random integer within the specified range.
    • If min is greater than max, the function returns -1.

char rand_choice_char(const char* arr)

Selects a random character from the provided character array.

  • Parameters:

    • arr: A pointer to a null-terminated character array.
  • Returns:

    • A randomly chosen character from the array.

long rand_choice_int(const int* arr, const int s)

Selects a random integer from the provided integer array.

  • Parameters:

    • arr: A pointer to an array of integers.
    • s: The size of the integer array.
  • Returns:

    • A randomly chosen integer from the array.

char rand_char(const char a, const char b)

Generates a random character between two given characters a and b.

  • Parameters:

    • a: The lower bound character.
    • b: The upper bound character.
  • Returns:

    • A random character between a and b.

Usage

  1. Include the stdrand.h header in your C source code.
  2. Call the appropriate functions based on your requirements.

Example

#include "stdrand.h"
#include <stdio.h>

int main() {
    long randomNum = rand_int(10, 20);
    printf("Random number between 10 and 20: %ld\n", randomNum);
    
    char choices[] = "ABCDEF";
    char randomChar = rand_choice_char(choices);
    printf("Random character: %c\n", randomChar);
    
    int numArray[] = {100, 200, 300, 400, 500};
    long randomIntArray = rand_choice_int(numArray, sizeof(numArray) / sizeof(int));
    printf("Random integer from array: %ld\n", randomIntArray);
    
    char randomGeneratedChar = rand_char('A', 'Z');
    printf("Random generated character between A and Z: %c\n", randomGeneratedChar);
    
    return 0;
}

License

This library is released under the MIT License.

stdrand.h's People

Contributors

farhanaliofficial avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.