Help generating one random number.

We are trying to generate a random number, for a password in our game, that will be created on the game's launch and will freeze itself so it is restrained to that number and that number only for the remainder of the game.

Here is our code right now for the random number:
This is the .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "randnum.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <conio.h>



void randnum()
{
using namespace std;
int random_integer;
for(int index=0; index<1; index++){
random_integer = (rand()%8999)+1000;
cout << random_integer << endl;
}
}


This is the header
1
2
3
4
5
6
7
8
9
// randnum.h
// #include <disclaimer.h>

#ifndef RANDNUM_H
#define RANDNUM_H

void randnum();

#endif  


We eagerly await your reply. =)
Hi Im new to this too, but I know that if you want a some what realistic random number you have to seed it before to loop:

srand ( time(NULL) );
Last edited on
We have previously included 'time' in our code, and that seemed to increase the random number by the amount of time spent in between inputs. We need a way to, once you get the number, freeze the number so we only have that one number for the rest of the program duration and make it so the user cannot overwrite the number by getting a different integer.

Still, all thank you for trying to help. =)
you need to start indenting your code, it makes it so much more readable.

for(int index=0; index<1; index++) is completely useless, it does nothing that the program wouldn't do if it wasn't there, you're just telling it to do everything in the loop once each time the function randnum() is called , which it would have done anyway

if you want your number to really be random, you will have to seed it like blaze said, this should be done only once in the entire program, so typically at the start of int main().

if you want it to only generate 1 random number, then you should only call it once, of course I'm guessing you can't only call it once because you are using the function to print out the random number

do you see what your real problem is now? you can't access the variable holding the random number without generating it all over again, read up on "pass by reference" as that will hopefully help you figure out what you need to do

if you're being taught this at some school, ask your teacher about scope, it seems you haven't understood it.
Last edited on
> We are trying to generate a random number, for a password in our game,
> that will be created on the game's launch and will freeze itself so it is
> restrained to that number and that number only for the remainder of the game.

Use a variable with a static storage duration to hold the random number.

2011 vintage:
1
2
3
4
5
6
7
8
9
#include <ctime>
#include <random>

int randnum()
{
   static std::mt19937 engine( std::time(nullptr) ) ;
   static int rand_int = std::uniform_int_distribution<>( 1000, 9999 ) (engine) ;
   return rand_int ;
}


1998 vintage:
1
2
3
4
5
6
7
8
9
#include <ctime>
#include <cstdlib>

int randnum()
{
    // seed the LCG in main()
   static int rand_int = std::rand() % 8999 + 1000 ;
   return rand_int ;
}
Topic archived. No new replies allowed.