Okay, so I'm writing this RPG game in C++, and I'm having some trouble with my random numbers (used primarily for the AI). I know how to use rand()%maxGoesHere and srand() with time (srand((unsigned)time(0))), but the whole thing's just getting repetitive and predictable- when it works. I've made a test of some random numbers (the terms are based on Python syntax, which is my most natural language (literally. More than English. Sometimes I think in Python, actually)), but as I said, they're a bit predictable. Here are the files.
main.cpp:
#include <iostream>
#include "random.h"
using namespace std;
int main ()
{
int i = random.randrange(11);
cout << i << endl;
i = random.randint(10, 21);
cout << i << endl;
char c;
cin >> c;
return 0;
}random.h:
#ifndef RANDOM_H_INCLUDED
#define RANDOM_H_INCLUDED
#include <cstdlib>
#include <ctime>
using namespace std;
class Random
{
void set_rand ();
public:
int randrange (int);
int randint (int,int);
} random;
void Random::set_rand ()
{
srand((unsigned)time(0));
}
int Random::randrange (int max)
{
#ifndef FIRST_TIME
#define FIRST_TIME
random.set_rand();
#endif
return (rand()%max);
}
int Random::randint (int min, int max)
{
#ifndef FIRST_TIME
#define FIRST_TIME
random.set_rand();
#endif
if (min < max)
{
return (rand()%(max-min)+min);
}
else
{
return 0;
}
}
#endifThat's my current random algorithm, but I feel like I need a better one.
If anyone can get their hands on the code in the Python random module, I could adapt that into C++. The Python random numbers are great.
Last edited by maxskywalker (2011-10-12 10:45:43)
Offline
poopo wrote:
If only you used java then I could help you.
![]()
Um, okay. If you know how to do it in Java, then I wouldn't mind your posting. I think I can adapt it. I really just have no idea how to do this. I can type C++ well enough. So let's have it.
Last edited by maxskywalker (2011-10-12 13:22:21)
Offline
maxskywalker wrote:
poopo wrote:
If only you used java then I could help you.
![]()
Um, okay. If you know how to do it in Java, then I wouldn't mind your posting. I think I can adapt it. I really just have no idea how to do this. I can type C++ well enough. So let's have it.
Ok so it's pretty lame and ineffective but here it is:
variable = Math.round(Math.random() * maxnumber);
That generates a pseudorandom number from 0 to maxnumber. you can also make a minumum.
Offline
poopo wrote:
maxskywalker wrote:
poopo wrote:
If only you used java then I could help you.
![]()
Um, okay. If you know how to do it in Java, then I wouldn't mind your posting. I think I can adapt it. I really just have no idea how to do this. I can type C++ well enough. So let's have it.
Ok so it's pretty lame and ineffective but here it is:
variable = Math.round(Math.random() * maxnumber);
That generates a pseudorandom number from 0 to maxnumber. you can also make a minumum.
Oh. Right. The problem with that is that it doesn't appear to use any changing variables, and in C++, doing the built-in random gets the same thing every time.
Offline
maxskywalker wrote:
poopo wrote:
maxskywalker wrote:
Um, okay. If you know how to do it in Java, then I wouldn't mind your posting. I think I can adapt it. I really just have no idea how to do this. I can type C++ well enough. So let's have it.Ok so it's pretty lame and ineffective but here it is:
variable = Math.round(Math.random() * maxnumber);
That generates a pseudorandom number from 0 to maxnumber. you can also make a minumum.Oh. Right. The problem with that is that it doesn't appear to use any changing variables, and in C++, doing the built-in random gets the same thing every time.
Well that useless why would they make that?
Offline
bbbeb wrote:
IMO Visual Basic is much better for random, as it includes the command Randomize.
And let me try this: this
most programming languages have a rand, rnd, and/or srand.
Offline
veggieman001 wrote:
bbbeb wrote:
IMO Visual Basic is much better for random, as it includes the command Randomize.
And let me try this: thismost programming languages have a rand, rnd, and/or srand.
Just realized that, lol.
I'm such a noob.
Hey:
do
srand(time(0));
And
rand()
when you want to do it
Last edited by bbbeb (2011-10-12 16:51:53)
Offline
bbbeb wrote:
IMO Visual Basic is much better for random, as it includes the command Randomize.
And let me try this: this
I know. Well, I didn't know about VB, but just about every programming language has some form of randomness. The C++ one's just not so good. Let me correct myself: it's absolutely terrible.
Offline
poopo wrote:
maxskywalker wrote:
poopo wrote:
Ok so it's pretty lame and ineffective but here it is:
variable = Math.round(Math.random() * maxnumber);
That generates a pseudorandom number from 0 to maxnumber. you can also make a minumum.Oh. Right. The problem with that is that it doesn't appear to use any changing variables, and in C++, doing the built-in random gets the same thing every time.
Well that useless why would they make that?
IDK.
Offline
bbbeb wrote:
max.
srand(seed)
![]()
What's that? I'll try it now.
Offline
bbbeb wrote:
srand(seed) uses the <ctime> library to induce a random seed into the randoms, and the rand() function launches it.
It just gives me an error about how seed isn't defined.
Offline