rand() Random Number Generation C++

Blade Nelson

I am trying to generate pseudo random ints using the rand() function. It works, but my problem is that it is always "selecting" the same name for the int. (In this case, it is 41. I think if you put the rand() in the while loop of main it is 85 or something.)

Is there a way to fix this? Here is my code:

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>

    int guess;
    int danse = rand() % 101;

    using namespace std;

    void more(){
    cout << "The number that you need to guess is higher!";

    return;
    }

    void lower(){
    cout << "The number that you need to guess is lower!";

    return;
    }

    int main(){
    while(1){
    cout << "\nGuess a number 0-100: ";
    cin >> guess;

    if (guess > danse){
        lower();
}
    if (guess < danse){
        more();
}

    if (guess == 101){
        break;
}

    if (guess == danse){
        cout << "\nYOU GUESSED IT. ARE YOU A WIZARD?! BECAUSE THAT IS PRETTY NEAT.";
        break;
}
    }
    }

Just a few side notes: Please don't try and tell me things I already know, like, explaining why I am using void functions, with return. Please, also do not try to convince me that using namespace std; is the "bad" way to do it. I understand that there are other ways of doing it. I choose not to.

Thanks!

simonc

You need to seed the random number generator by calling srand once before using rand. The current time is a cheap and easy way to choose a seed that varies between runs of your program.

int danse;

int main(){
    srand(time(NULL));
    danse = rand() % 101;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - random number generation

From Dev

Is random number generation in multiple threads using C++11 random library slow just like using rand() in multiple threads?

From Dev

Is random number generation in multiple threads using C++11 random library slow just like using rand() in multiple threads?

From Dev

On the random number generation dSFMT performance on C++

From Dev

Pseudo-random number generation in MEX C

From Dev

Efficient random number generation with C++11 <random>

From Dev

Spark - Random Number Generation

From Dev

Random number Generation with Seed

From Dev

Random number generation with in a range

From Dev

generation of random number in MATLAB

From Dev

Random number generation algorithm

From Dev

generation of random number in MATLAB

From Dev

Random number generation in Solaris?

From Dev

pseudo random number generation

From Dev

Random Number generation with GameplayKit

From Dev

Random Number Generation in R

From Dev

Random number generation with seeds

From Dev

asterisk random generation using rand in php

From Dev

C++ thread-safe uniform distribution random number generation

From Dev

How to use Mersenne Twister random number generation library in C?

From Dev

Grokking Random Number Generation in Haskell

From Dev

Random number generation on card and on terminal?

From Dev

Understanding uniform random number generation

From Dev

Random Number Generation in SQL Server

From Dev

Using engines for random number generation

From Dev

Random number generation using srand()

From Dev

Role of seed in random number generation

From Dev

User trustable random number generation

From Dev

Constraining random number generation in Python

Related Related

HotTag

Archive