Random numbers to stay under 200

This is a discussion on Random numbers to stay under 200 within the C Programming forums, part of the General Programming Boards category; Hey guys Here is my code (it is a function as usual with my crap) Code: void fPlanes(FILE *Fplanes) { ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    31

    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.
    Last edited by Salem; 11-20-2005 at 03:52 PM. Reason: Fixing code tags

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    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. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote 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. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    should I put a counter in?

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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.

    Quote 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);
    Last edited by Cactus_Hugger; 11-20-2005 at 05:21 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote 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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Quote 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.
    Quote Originally Posted by ssharish2005
    Code:
    printf("\n%d\n",fprintf(fp,"%i\t%i\t%i\n ",array1[i],array2[i],array3[i]));
    *scratches head* Must stop before I get a head-ache...

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 02:22 PM
  2. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 03:01 PM
  3. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 01:59 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21