Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <time.h>
  5. int main(int argc, char* argv[]){
  6. clock_t start = clock();
  7. int i=0;
  8. float xn =1.0;
  9. int a =573432;
  10. float n =2e31;
  11. long points=1e5;
  12. float* numsx =malloc(points * sizeof(float));//allocate mem so larger arrays can be used
  13. float* numsy =malloc(points * sizeof(float));
  14. float xn1=0.0;
  15. int arrayx=0;
  16. int arrayy=0;
  17. int y=1;
  18. int j=0;
  19. for (i=0; i< (2*points +1); i++){
  20. xn1=fmod((a*xn),n);//eqn whitch gens the rendom nums
  21. if (y==1){
  22. numsx[arrayx]=xn1;//adds to x array and y array alternativly
  23. arrayx++;
  24. }
  25. if (y==-1){
  26. numsy[arrayy]=xn1;
  27. arrayy++;
  28. }
  29. y= -y;
  30. xn =xn1;
  31. }
  32. for(j=0; j < 500; j++){
  33. printf("%f",numsx[j]);
  34. }
  35. clock_t finish =clock();
  36. double timeused;
  37. timeused=(finish-start)/CLOCKS_PER_SEC;//time taken to calc
  38. printf("\n%lf\n", timeused);
  39. FILE *file;
  40. file =fopen("randomnums.dat", "w");
  41. if (file == NULL) {
  42. printf("I couldn't open results.dat for writing.\n");//fails if file cant be opened
  43. exit(0);
  44. }
  45. for(i=0; i< (points/2); i++){
  46. fprintf(file, "%f %f\n", numsx[i], numsy[i]);//wirtes to file in format: x space y
  47. }
  48. fclose(file);//must remember to close file and relese the memory from malloc
  49. free(numsy);
  50. free(numsx);
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement