@
Inphinity If the thread is solved please ask to a minion close it else it can help.
Random struct :
Code:
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
struct Random {
Random();
Random(int);
int Next();
int Next(int);
int Next(int , int );
double NextDouble();
char* NextBytes(char[]);
char* NextBytes(int len);
char* NextCharArray(int len);
private:
int Seed;
};
Random::Random(){
Seed = (int) time(0);
}
Random::Random(int seed){
Seed = seed;
}
int Random::Next() {
return rand() % RAND_MAX;
}
int Random::Next(int nxt) {
nxt++;
return rand() % nxt ;
}
int Random::Next(int min, int max) {
if(min > max) // If min > max then swap their values
{
int temp = max;
max = min;
min = temp;
}
return (rand() % (max-min + 1)) + min;
}
double Random::NextDouble() {
//return (double)((rand()%10 + 1)*0.1) ; Returns exactly values e.g : 0.1 , 0.4 , 0.8
return static_cast<double>(rand())/RAND_MAX;
}
char* Random::NextBytes(char array[]) {
string s;
for(int i = 0; i < strlen(array);i++)
{
int aids = rand() % 255 + 1;
char buffer[16];
sprintf(buffer,"%d",aids);
s += buffer;
}
return (char*) s.c_str();
}
char* Random::NextBytes(int len ) {
string s;
for(int i = 0; i < len;i++)
{
int aids = rand() % 255 + 1;
char buffer[16];
sprintf(buffer,"%d",aids);
s += buffer;
}
return (char*) s.c_str();
}
//Credits of generating char array go to Jason
char* Random::NextCharArray(int len) {
char *str = (char*)calloc(len + 1, sizeof(char));
for (int i = 0; i < len; i++)
str[i] = (char)(rand() % 0x5E + 0x21);
return str;
}
Example code ( stupid but still a example ) :
Code:
#include <iostream>
#include <random.h>
using namespace std;
void PlayAtLocation(int, int[]);
int main() {
Random randx = Random(777);
int board[] = { 0,0,0,0,0,0,0,0,0};
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
PlayAtLocation(randx.Next(0,9), board);
cin.get();
cin.get();
return 0;
}
void PlayAtLocation(int location,int bd[]) {
if(bd[location] == 0 ) {
bd[location] = 1;
cout << "Played at location: " << location;
} else {
cout << "Already played at this location! (" << location <<")";
}
}