Random float

    0

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Generate a random float

    #include <iostream>
    #include <random>
    using namespace std;
    
    float randFloat(int hi = 0, int lo = 0){
      if(hi == 0){
        //random number in [0,1)
        return static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
      }else if(lo == 0){
        //random number in [0,hi)
        return static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/hi));
      }else{
        //random number in [lo, hi)
        return lo + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(hi-lo)));
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.