c++ - Random generator for guess game -
i've been searching better solution own , haven't been able find 1 understand or works me.
i have made simple game computer randomly generates number guess number , if higher computer says higher , on..
the problem randomly generated number, after looking alot of information regarding <random>
, uniform_int_distribution
, default_random_engine
. have found out computer generates random number, if run program again same random number generated.
my solution:
uniform_int_distribution<unsigned> u(0,100); // code randomly generate numbers between 0 , 100 default_random_engine e; // code randomly generate numbers size_t userinput; // user input find out in vector vector<int> randomnumbers; //vector hold random numbers unsigned start = 0, ending = 101, cnt = 0; // used in game not important right cout << "please enter number between 1 , 1000 randomness" << endl; cin >> userinput; for(size_t = 0; < 1000; ++i){ //for loop push numbers vector randomnumbers.push_back(u(e)); } unsigned guess = randomnumbers[userinput]; // number user have guess in game
my solution right use vector push alot of randomly generated numbers in ask user type number computer uses game. there should better way of doing this. , question therefore
is there better way randomly generate numbers use in game?
either use std::random_device
in place of std::default_random_engine
, or else think of way provide different number engine each time run.
this number called "seed" , can passed optional parameter constructor. since std::default_random_engine
implementation-specific, , different engines different things seeding, want choose specific engine if you're providing seed. deterministic pseudo-random number generator produce same sequence of outputs given seed, want use different seed each time.
for no-security uses guessing game, "obvious" thing use seed current time. speaking different each time program run, although if can run program twice in less granularity of clock that's not case. using time seed random engine pretty limited job toy program.
Comments
Post a Comment