SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
C++

C++ static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your C++ code

  • All rules 674
  • Vulnerability13
  • Bug139
  • Security Hotspot19
  • Code Smell503

  • Quick Fix 91
Filtered: 2 rules found
user-experience
    Impact
      Clean code attribute
        1. Facilities in <random> should be used instead of "srand", "rand" and "random_shuffle"

           Code Smell
        2. Assignment operators should return non-"const" reference to the assigned object

           Code Smell

        Facilities in <random> should be used instead of "srand", "rand" and "random_shuffle"

        intentionality - clear
        maintainability
        Code Smell
        • user-experience
        • since-c++11

        Why is this an issue?

        The use of srand together with rand to seed the random number generator and then generate numbers usually produces low-quality randomness. Further, rand can only provide a number between 0 and RAND_MAX, and it is left to the caller to transform the result into what is actually required (E.G. a float between 0 and 1 for a random percentage, an int between 1 and 6 for a dice game, …​), and that transformation might introduce additional biases.

        C++11 introduced the <random> library, which contains several high quality random value generators as well as statistical distributions you can use to put the results in the form you need. Those mechanisms should be used instead of rand and srand.

        Additionally, std::random_shuffle, which is deprecated in C++14 and removed in C++17, uses rand and should be replaced by std::shuffle, which uses the random number generators provided by <random>.

        Noncompliant code example

        #include <stdlib.h>
        #include <algorithm>
        // ...
        
        void f() {
          srand(time(nullptr)); // Noncompliant
          vector<int> v;
          int size = rand() % 1000 + 1000; // Noncompliant, note that this way of coercing the result introduces extra bias
          for (auto i = 0; i < size; ++i) {
            v.push_back(i);
          }
          random_shuffle(v.begin(), v.end()); // Noncompliant
          for (auto i : v) { cout << i << " "; }
        }
        

        Compliant solution

        #include <algorithm>
        #include <random>
        // ...
        
        void f() {
          random_device rd;  // Will be used to obtain a seed for the random number engine
          mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
          uniform_int_distribution<> dis(1000, 1999); // Same distribution as before, but explicit and without bias
          vector<int> v;
          for (auto i = 0; i < dis(gen); ++i) {
            v.push_back(i);
          }
          shuffle(v.begin(), v.end(), gen);
          for (auto i : v) { cout << i << " "; }
        }
        
          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use