Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Random number to an int (c++)

I want to give my "int bValue" a random number from 1-50, and have that number stored so that the next time I print "bValue" I get that same number which was generated before.
I tried doing this on my own and using google but no success. I haven't been coding for very long as you can tell.

Grateful for any help.
💜GOMINING💜
Can you show us the code you currently have?
Code
// C style pseudo-random
#include <cstdlib>
#include <ctime>

// seed current time
  srand(time(0));
// generate number
  int bValue = rand() % 51;
// print number
  printf("Pseudo-random number: ", bValue);

Your phrasing is weird to me. Sounds like you just want to be able to print it later on. If that's the case, store it in an int (or "unsigned int" to eliminate the possibility of somehow getting a negative) and print the int whenever you need. IIRC the return value of rand() is an int, but otherwise just typecast it.
[Image: chain.png]
Another solution. Usually I use a Mersenne-Twister. Another way of generating a random number.


Code
#include <iostream>
#include <random>

using namespace std;

int main()
{
    // C++ - Generate rndm. num
    random_device seed_generator;
    mt19937_64 random_device(seed_generator());
    uniform_int_distribution<int> distribution(1, 50);

    // stored in bValue1
    const int bValue1 = distribution(random_device);

    // print first
    cout << bValue1 << endl;

    // do sth.

    // print sec.
    cout << bValue1 << endl;
}

I think this follows the steps you already mentioned in your question.
(Apr 12, 2021 10:46 AM)Sajuuk Wrote:  
Code
// C style pseudo-random
#include <cstdlib>
#include <ctime>

// seed current time
  srand(time(0));
// generate number
  int bValue = rand() % 51;
// print number
  printf("Pseudo-random number: ", bValue);

Your phrasing is weird to me. Sounds like you just want to be able to print it later on. If that's the case, store it in an int (or "unsigned int" to eliminate the possibility of somehow getting a negative) and print the int whenever you need. IIRC the return value of rand() is an int, but otherwise just typecast it.

Sorry about that, but thanks now it works.
The issue I had was that the code below would always run the 'else' regardless if 'bValue == gValue'. So I assumed the random generator kept giving 'int bValue' a new number or something.
I'm new at this just trying to learn.

Code
if(bValue == gValue) {
cout << dock << endl;
} else {
cout << "The number was incorrect." << endl;
}
💜GOMINING💜
(Apr 12, 2021 12:30 PM)MHC-1 Wrote:  Another solution. Usually I use a Mersenne-Twister. Another way of generating a random number.


Code
#include <iostream>
#include <random>

using namespace std;

int main()
{
// C++ - Generate rndm. num
random_device seed_generator;
mt19937_64 random_device(seed_generator());
uniform_int_distribution<int> distribution(1, 50);

// stored in bValue1
const int bValue1 = distribution(random_device);

// print first
cout << bValue1 << endl;

// do sth.

// print sec.
cout << bValue1 << endl;
}

I think this follows the steps you already mentioned in your question.

This is the way you're supposed to do it in C++, the other suggested way in earlier post is for C.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Will PattersonSep 9, 2021 08:45 AM 6 195 Sep 9, 2021 08:45 AM
Last Post: ThinWhiteDuke
[Python] by madeha elsayedxxxxMay 17, 2020 10:01 PM 1 142 May 17, 2020 10:01 PM
Last Post: District
[Python] by Fivepoint0Oct 23, 2018 05:24 PM 3 191 Oct 23, 2018 05:24 PM
Last Post: Fivepoint0