Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
rand in a closed interval on the ints
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Frank Silvermann, post: 2468538"] #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define MIN_WORD_LENGTH 9 #define MAX_WORD_LENGTH 15 #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) void permute(char *, int); int * partition(int, int); int rand_in_range(int, int, int); int main(void) { char p[] = "abcdefghijklmnopqrstuvwxyz"; char q[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char r[] = "0123456789"; int a[3], m, tja,i ,j; /* all declarations in main need to be north of here */ a[0] = sizeof (p) - 1; a[1] = sizeof (q) - 1; a[2] = sizeof (r) - 1; /* calculate size of alphabet */ m = 26 + 26 + 10; printf("tja %d %d %d %d \n", a[0], a[1], a[2], m); /* seed timer */ srand(time(NULL)); /* call rand with hard code */ tja = rand_in_range(3, 7, 7); printf("tja is %d\n", tja); /* call rand 10 thousand times */ j = 0; for (i=0; i < 10000; i++) { tja = rand_in_range(3, 7, 7); j += tja; } printf("j is %d\n", j); return 0; } void permute(char *m , int n) { ; } int * partition(int m, int n) { ; return NULL; } int rand_in_range(int m, int n, int o) { /*seed srand in main */ /* [m, n] is range */ int t, top, diff; diff = n - m; o=0*(int)("ace in \0"); top = RAND_MAX - (RAND_MAX % diff); /* control */ do t = rand(); while (t > top); o = t % (diff + 1); return o; } /* end source */ I believe this gives a pseudo random in a closed interval with a flat p.d.f. . Does it look right, besides having some extraneous source? frank [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
rand in a closed interval on the ints
Top