Is there a better random function than rand()?

Non-platform specific questions.

Is there a better random function than rand()?

Postby lverona » Wed Jun 27, 2012 9:25 am

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?
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

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

Postby Game A Gogo » Wed Jun 27, 2012 1:52 pm

why isn't it a good randomizer?
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

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

Postby lverona » Thu Jun 28, 2012 6:34 am

I tried it out in an example such as rand(5). It very frequently gives the same value, like 2,2,2,2,2.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

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

Postby schnellboot » Thu Jun 28, 2012 10:33 am

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?
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

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

Postby Game A Gogo » Thu Jun 28, 2012 11:56 am

can you show me the piece of code where you try to use rand()?
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

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

Postby skydereign » Thu Jun 28, 2012 6:26 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby schnellboot » Thu Jun 28, 2012 9:32 pm

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
}
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

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

Postby skydereign » Thu Jun 28, 2012 9:35 pm

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);
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby schnellboot » Thu Jun 28, 2012 9:40 pm

yay thanks it worked :)

ps you forgot to change temp to buffer
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron