Page 1 of 1

Is there a better random function than rand()?

PostPosted: Wed Jun 27, 2012 9:25 am
by lverona
Hey fellas!

rand() does not seem to be very good random number generator. Is there anything else available? Maybe some other function in C which I can use?

Re: Is there a better random function than rand()?

PostPosted: Wed Jun 27, 2012 1:52 pm
by Game A Gogo
why isn't it a good randomizer?

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 6:34 am
by lverona
I tried it out in an example such as rand(5). It very frequently gives the same value, like 2,2,2,2,2.

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 10:33 am
by schnellboot
this might be a bit off topic
but I made a test actor to test rand()
test -> Create Actor -> Script Editor

Code: Select all
int i=0;
for(i=0;i<50;i++)
{
    int rndm = rand(5);
    sprintf(text, "%s, %i", text, rndm); //add the last random number to the whole string
}


why doesn't this work?

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 11:56 am
by Game A Gogo
can you show me the piece of code where you try to use rand()?

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 6:26 pm
by skydereign
schnellboot wrote:this might be a bit off topic
but I made a test actor to test rand()
test -> Create Actor -> Script Editor

Code: Select all
int i=0;
for(i=0;i<50;i++)
{
    int rndm = rand(5);
    sprintf(text, "%s, %i", text, rndm); //add the last random number to the whole string
}


why doesn't this work?

That wouldn't work because you are writing into a string that you are accessing. It is kind of like doing this.
Code: Select all
int var = 0;
var = var++;

What should var equal? It is undefined behavior in c. So what you would need to do is create a temporary buffer, store text, and write text with the temporary buffer.

lverona wrote:I tried it out in an example such as rand(5). It very frequently gives the same value, like 2,2,2,2,2.

rand acts just like the rand in c, which really won't get much better. The only difference is that rand in gE takes a variable, but that is the same as doing rand()%variable in real c. If you wanted to get potentially more diverse answers you could try rand(1000)%5, but for the most part those should achieve the same result. You could make your own random function, that you could force it to avoid that kind of behavior, but then it has the potential to be diversely random, instead of actually random.

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 9:32 pm
by schnellboot
sky this still doesn't work

Code: Select all
int i=0;
for(i=0;i<50;i++)
{
    int rndm = rand(5);
    sprintf(temp, "%s, %i", text, rndm); //add the last random number to the whole string
    text = temp; //temp is a global string variable
}

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 9:35 pm
by skydereign
schnellboot wrote:sky this still doesn't work

Code: Select all
int i=0;
for(i=0;i<50;i++)
{
    int rndm = rand(5);
    sprintf(temp, "%s, %i", text, rndm); //add the last random number to the whole string
    text = temp; //temp is a global string variable
}

That's because that isn't what I said, and you can't assign strings like that (you can't say one string equals another).
Code: Select all
int i;
char temp[255];

for(i=0;i<50;i++)
{
    int random = rand(5);
    sprintf(temp, "%s, %d", text, random);
    strcpy(text, temp);
}

Re: Is there a better random function than rand()?

PostPosted: Thu Jun 28, 2012 9:40 pm
by schnellboot
yay thanks it worked :)

ps you forgot to change temp to buffer