Thread: Random numbers to stay under 200

1. Random numbers to stay under 200

Hey guys Here is my code (it is a function as usual with my crap)
Code:
```void fPlanes(FILE *Fplanes)
{
/*declaring arrays, I am going to use int to keep everything standard */
int aXaxis[50];
int aYaxis[50];
int aZaxis[50];
int q = 0;

aXaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;
aYaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;
aZaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;

for(q=0; q < 50; q++)
{
fprintf(Fplanes, "%i %i %i \n", aXaxis[q], aYaxis[q], aZaxis[q]);
}
}```
As you can see this creates a file with 3 columns of numbers, I am trying to get the numbers under 200 (or any number set that I want) that equation above is suppose to keep them above 0 and less then too but it does is keep the numbers under 200, and it can be way under like -18921341243, How do I make each of these arrays be between 0 and 200 or 1 and 200?

I know my syntax is right.

2. if it helps, Here is a copy of the text file:

-1878016948 0 0
1 66752 0
-1073744568 -1881138584 69644
-1878016840 -1073743728 61440
2 0 6
-1769496876 69284 20
0 -1073743716 1915905074
-1073743096 28 5
8 1919164426 -1881131436
1 28 -1073743456
-1073743092 2185 0
-1073743840 0 -1881131096
66512 69348 0
-1073743840 -1073743592 0
0 5 0
-1881053704 1 -1073743096
0 6 -1073743084
0 1684107892 -1073743092
0 1744833792 -1610583185
-1073743096 0 1819213832
8 1932 -1073743084
-1610604580 69644 -1073743092
-1610605624 -1881073184 -1610554928
69352 -1073743600 850045863
-1878391332 -1610549856 0
-1073743840 -1881072476 0
-1073743092 -1881119204 0
-1073743768 -1073743632 0
-1073743712 0 0
-268435456 -1769492588 0
0 23456 0
214748364 1818585441 1784767074
-858993460 5960 1627389952
0 -1879040756 0
23450 -1880805376 0
-1881094536 3146092 -1881053704
-1881009952 50352 75684
-1073743664 -1073743496 0
-1073743672 -1073743084 0
-1073743812 -1073743092 -1073743096
75756 -1880801884 8
1050560 1050560 69144
1051904 -1881129564 8
-1880805376 25697 -1073776273
-1881075004 1920231424 1684078600
1 75684 -1073743360
-1073743092 6 1
-1073743840 5 -1878966476
66512 20 69008
-1073743360 0 -1073743084

3. Originally Posted by sloopy
Code:
` aXaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;`
This line assigns to the fiftieth element of aXaxis (which is not even part of the array, since indices run from 0 to 49). Your for loop is printining out uninitialized values.

4. should I put a counter in?

5. As others have said, you need to loop through your indecies, and not try to do [50]. C & C++ arrays are zero-indexed, so they start at zero and go to length-1.

Originally Posted by sloopy
Code:
`(int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;`
That can be simplified, greatly, and look a lot less confusing. I see no point in (double)(1)... a simple 1.0 should suffice. You can also drop the parenthesis around RAND_MAX, they do nothing. Casting in pretty high in precedence, it'll happen before + - * /. Once you do that, you'll see you're casting it to an (int) before multiplying by 200, not what you wanted. So, after fixing that up:
Code:
`(int) ((double) rand() / ((double) RAND_MAX + 1.0) * 200.0);`
I don't think you need that final (int) either, since you're assigning it to an int, that cast should occur anyways.

Edit: Your formula in itself is broken. If rand() returns 0, you'll get zero, not what you want. I suggest multiplying by 199.0 (That's the number of numbers between 0 and 200, aka, the size of your range) and adding 1 (the offset of your range) to the final result.

Code:
`1 + ((double) rand() / ((double) RAND_MAX + 1.0) * 199.0);`

6. Originally Posted by sloopy
aXaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;
aYaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;
aZaxis[50] = (int)(((double)rand())/((double)(RAND_MAX)+(double)(1)))*200;
u are just inserting a value only at the last elment of th array . though the array indexing is not correct. Cactus_Hugger and Rashakil Fol has explained why. here is a sample code which give u an idea of getting a randon no. using the library function - srand()

Code:
```#include<stdio.h>
#include<stdlib.h>

int main()
{
FILE *fp;
int array1[10];
int array2[10];
int array3[10];
int i=0;

srand(time(NULL));
if((fp=fopen("test102.txt","w"))!=NULL)
{
for(i=0;i<9;i++)
{
array1[i]=rand() % 200;
array2[i]=rand() % 200;
array3[i]=rand() % 200;
printf("\n%d\n",fprintf(fp,"%i\t%i\t%i\n ",array1[i],array2[i],array3[i]));

}
}
else
{
printf("Error: File cannot be opened\n");
getchar();
exit(1);
}
fclose(fp);

getchar();
return 0;
}

/*
myoutput text file text102.txt
173	41	40
19	19	113
112	148	174
141	36	61
75	126	179
42	172	11
97	129	168
172	105	124
73	1	103
*/```
u can see the randon values with in 200

ssharish2005

7. Originally Posted by ssharish2005
u are just inserting a value only at the last elment of th array .
No. It's being inserted after the last element in the array, as described above.
Originally Posted by ssharish2005
Code:
`		for(i=0;i<9;i++)`
You on the other hand, aren't putting anything in the last element of yours.
Originally Posted by ssharish2005
Code:
`printf("\n%d\n",fprintf(fp,"%i\t%i\t%i\n ",array1[i],array2[i],array3[i]));`

Quzah.

8. Code:
`printf("\n%d\n",fprintf(fp,"%i\t%i\t%i\n ",array1[i],array2[i],array3[i]));`
here is was just checking out the return value for the purpose of debugging. ignore the printf, its just
Code:
`fprintf(fp,"%d\t%d\t%d\n",array1[i],array2[i],array3[i]);`
and the for loop should be

Code:
`for(i=0;i<=9;i++)`
thax quzah for pointing it out

ssharish2005