This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-12 09:23:21

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Random Algorithm

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:

Code:

#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:

Code:

#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;
    }
}

#endif

That'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

 

#2 2011-10-12 10:23:42

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Offline

 

#3 2011-10-12 13:03:10

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Random Algorithm

If only you used java then I could help you.  hmm


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#4 2011-10-12 13:21:46

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Random Algorithm

poopo wrote:

If only you used java then I could help you.  hmm

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

 

#5 2011-10-12 13:27:07

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Random Algorithm

maxskywalker wrote:

poopo wrote:

If only you used java then I could help you.  hmm

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.


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#6 2011-10-12 13:29:57

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Random Algorithm

poopo wrote:

maxskywalker wrote:

poopo wrote:

If only you used java then I could help you.  hmm

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

 

#7 2011-10-12 13:41:01

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Random Algorithm

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?


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#8 2011-10-12 15:59:33

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Random Algorithm

IMO Visual Basic is much better for random, as it includes the command Randomize.

And let me try this: this


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#9 2011-10-12 16:00:37

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Random Algorithm

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.


Posts: 20000 - Show all posts

Offline

 

#10 2011-10-12 16:51:33

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Random Algorithm

veggieman001 wrote:

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.

Just realized that, lol.

I'm such a noob.


Hey:
do

Code:

srand(time(0));

And

Code:

rand()

when you want to do it  big_smile

Last edited by bbbeb (2011-10-12 16:51:53)


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#11 2011-10-12 18:10:21

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Random Algorithm

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

 

#12 2011-10-12 18:11:55

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Random Algorithm

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

 

#13 2011-10-12 18:13:25

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Random Algorithm

max.

srand(seed)

big_smile


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#14 2011-10-12 19:01:13

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Random Algorithm

bbbeb wrote:

max.

srand(seed)

big_smile

What's that?  I'll try it now.

Offline

 

#15 2011-10-12 19:02:13

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Random Algorithm

srand(seed) uses the <ctime> library to induce a random seed into the randoms, and the rand() function launches it.


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#16 2011-10-12 20:30:25

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Random Algorithm

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

 

#17 2011-10-23 15:04:48

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Offline

 

Board footer