Im already generating numbers correctly from 0 to MAX_INT. But Id like to know how can I change that range (including negetive numbers)

And while already on the subject, why does this code only generate numbers in the VERY high 90s?

int main()
    int array[100];
    return 0;

void gen (int *array)
    int i;
    for (i=0;i<=100;i++)

Probably because you're assigning an element past the end of the array, and there's a variable there that throws off your code unpredictably.

Also, you've already showed a way to change that range; I don't see what the problem is.

But generally speaking the code can be slightly improved. Some random number generators in bad versions of standard libraries will be not very random when you take the result modulo some small number. So using division is.. recommended. ((int) (100 * (rand() / (1.0 + RAND_MAX)))) seems to be a recipe. Of course, this is a lot uglier -- we use floating point arithmetic to avoid risking overflowing int.

With decent random number implementations, you could just use modulo.

Making a change to RF's equation will give you the ability to get random numbers in any range. Add a start and a range variable: ((int) (range * (rand() / (1.0 + RAND_MAX))) + start) So, if you want numbers from (and including) 3 through 10, start is 3, range is 8
For numbers from -100 through 100, start is -100, range is 201

Thanks WaltP but I dont get your function too well. Sorry.

I understand that "Start" is the number (including itself) I want to start from. But I fail to see range as in the first example the range is -2 the actual numbers till I want it but the next example is double+1 the actual number.

From tests, it seems range just tells you how many numbers it generates.

If I put range 5, it generates 5 numbers which are 0,1,2,3 and 4.

Is this correct?

Hmm Im not sure if I should start another topic since this is a different topic but:

Right now I was using:


How can I adapt that to

n=((int) (5 * (rand() / (1.0 + RAND_MAX))) + 0) ;

[s]Maybe this:

n=((int) (5 * (rand() / ((1.0+3*numberofplayers) + RAND_MAX))) + 0) ;


Nope, doesnt seem to work...

Hmm I think what Im looking for is:

n=((int) ((1+(3*numberofplayers)) * (rand() / (1.0 + RAND_MAX))) + 0) ;

Yes. In general, you should make a function

int random(int n) {
    return (int) (n * (rand() / (1.0 + RAND_MAX)));

and use that to generate a random number from 0 to n-1. With a decent random number generator, it's as good as rand() % n , but with a typically bad random number generator (which you might have) it gives better pseudorandomness. As with rand() % n , it's only good when n is significantly less than RAND_MAX -- how much less depends on what you're using the random number for -- if you do the math yourself, you'll see how different values have different probabilities of getting returned.

n=((int) ((1+(3*numberofplayers)) * (rand() / (1.0 + RAND_MAX))) + 0) ;

Works pretty well :) numberofplayers is a global variable so I dont need to pass it.

Thank you very much to everyone who has helped out.

I still don't get why you bother adding zero.

I still don't get why you bother adding zero.

Because I needed 0.

>Because I needed 0.
Adding zero does nothing. Do you remember your elementary school arithmetic?

1 + 0 = 1
2 + 0 = 2
3 + 0 = 3
N + 0 = N

Because I needed 0.

You're mistaking zero for Zorro. Get it right.