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.