My Random Struct

Posts 1–15 of 16 · Page 1 of 2
My Random Struct
Hello guys , just posting my struct for random number generator
it's simple and I get inspiration from C++/CLI random class, here is it:
Code:
#include <cstdlib>

struct Random {
Random();
Random(int);
int Next();
int Next(int);
int Next(int , int );
double NextDouble();  
void NextBytes(char*);

private:
       int Seed;   
};

Random::Random(){
 Seed = 0;                
}

Random::Random(int seed){
Seed =  seed;                 
}

int Random::Next() { 
srand ( Seed  );
return rand() % RAND_MAX;   
}

int Random::Next(int nxt) {
srand ( Seed  );
nxt++; 
return rand() % nxt ;                 
}

int Random::Next(int min, int max) {
srand ( Seed  );
if(min > max) // If min > max then swap their values
{
       int temp = max;
       max = min;
       min = temp;
}
return (rand() % (max-min)+min);
}

double Random::NextDouble() { 
 srand ( Seed  ); 
//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;             
}

void Random::NextBytes(char* array) {
     
srand ( Seed );

}
Note: If you guys could help me with the NextBytes function
I tried everything and can't make it work like:
Code:
for(int i = 0; i < strlen(array); i++)
{
int randed = rand()% 256 + 1;
array[i] = (char) randed; // dont work
array[i] = static_cast<char>(randed); // dont work
array[i] = reinterpret_cast<char>(randed); // dont work
array[i] = boost::lexical_cast<char>(randed)  // dont work with great numbers
}
So this is it, appreciate!
The strlen of an empty array will be 0 if the array is initialized to 0.

add a length param to it, or initialize the array to a different value.
Quote Originally Posted by Hell_Demon View Post
The strlen of an empty array will be 0 if the array is initialized to 0.

add a length param to it, or initialize the array to a different value.
My problem isn't with array size, the problem is converting from int to char
Quote Originally Posted by KissU View Post
My problem isn't with array size, the problem is converting from int to char
Why's the first one a problem?

Code:
	int lul = 123;
	char durr[16];
	durr[3] = (char) lul;
Works fine
Quote Originally Posted by Nico View Post


Why's the first one a problem?

Code:
	int lul = 123;
	char durr[16];
	durr[3] = (char) lul;
Works fine
Yeah works fine, It prints "{".Should print "123". ;P
Quote Originally Posted by KissU View Post
Yeah works fine, It prints "{".Should print "123". ;P
Naw it shouldn't. If you want it to print 123, use sprintf.
Quote Originally Posted by Nico View Post


Naw it shouldn't. If you want it to print 123, use sprintf.
But sprintf is for char*.
I want to do like :
Code:
 
array[0] = "123";
But I don't know how to make it with a int.
beacuse
char durr[16] is char*;
dur[0] is char;
Can someone help ;P?
Quote Originally Posted by KissU View Post
But sprintf is for char*.
I want to do like :
Code:
 
array[0] = "123";
But I don't know how to make it with a int.
beacuse
char durr[16] is char*;
dur[0] is char;
Can someone help ;P?
Then you need a char* array.

char* durr[16];
Quote Originally Posted by Nico View Post


Then you need a char* array.

char* durr[16];
I thinked it too, so another question how i get the size of that string ? (char**)
Quote Originally Posted by KissU View Post
I thinked it too, so another question how i get the size of that string ? (char**)
char** isnt a string, thats a pointer to a string.
Can you help me a little with this,
I made a little search but i'm a bit confuse about it.
Quote Originally Posted by KissU View Post
Can you help me a little with this,
I made a little search but i'm a bit confuse about it.
Okay. You have random numbers and want a string of all of them. I'd do it this way:

Code:
string numbers;

for (...)
{

   int num = random(x,y);
   char numstr[16];
   sprintf(numstr,"%i",num);
   numbers += numstr;

}
Quote Originally Posted by Nico View Post


Okay. You have random numbers and want a string of all of them. I'd do it this way:

Code:
string numbers;

for (...)
{

   int num = random(x,y);
   char numstr[16];
   sprintf(numstr,"%i",num);
   numbers += numstr;

}
void Random::NextBytes(const char array[]) {

srand ( Seed );
string s;
for(int i = 0; i < strlen(array);i++)
{
int aids = rand() % 255 + 1;
char buffer[16];
sprintf(buffer,"%d",aids);
s += buffer;
}

array = s.c_str();
}

Something like this ?

---------- Post added at 08:43 PM ---------- Previous post was at 06:45 PM ----------

Finished structure.

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[]);

private:
       int Seed;   
};

Random::Random(){
 Seed = Next();                
}

Random::Random(int seed){
Seed =  seed;                 
}

int Random::Next() { 
srand ( Seed  );
return rand() % RAND_MAX;   
}

int Random::Next(int nxt) {
srand ( Seed  );
nxt++; 
return rand() % nxt ;                 
}

int Random::Next(int min, int max) {
srand ( Seed  );
if(min > max) // If min > max then swap their values
{
       int temp = max;
       max = min;
       min = temp;
}
return (rand() % (max-min)+min);
}

double Random::NextDouble() { 
 srand ( Seed  ); 
//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[]) {
     
srand ( Seed );
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();
}
Thanks , works fine.
Posts 1–15 of 16 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us