Randomly Picking from Arrays?

Started by
25 comments, last by Inmate2993 19 years, 3 months ago
Well, I've found a, "workaround" for this problem. I'm totally getting rid of the array, and instead of doing this stuff, I'm going to save the first number (Which, well, will always be 1), and i'm going to save the last number, and i'm going to do :

int A = rand()%End;       //End is saved earlier onreturn A;


But, then I was wondering if I can specify where to start, as well as where to end? Example :

int A = rand()%Start%End;



Would that one work? I don't want to take the chances of drawing a 0, but then again, I guess if I got a 0, I could just redraw until A ! = 0. Thanks for your help, ya'll. Later! :P
Advertisement
OK, let's say you want a number between 3 and 6, inclusive. So, I would do something like this (my syntax may not be perfect here... Long night)
int X = 6;  // The higher numberint Y = 3;  // The lower numberint Z = ((rand()%((X + 1) - Y)) + Y);

Bah, that is an ugly line of code.. Anyway, basically (and this disection of the code assumes the numbers 6 and 3 for X and Y) the '(rand()%((X + 1) - Y))' will return a value from 0 to 3. Then, you add Y, in this case 3, thus giving you a number from 3 to 6. Clear enough?
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Couldn't you get the size of an array (as in number of entries) by using sizeof( array ) / sizeof( array[0] ) ?
I've seen that snippet in MS statusbar pane filler code. This assumes that you have at least one entry of course.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

That works for arrays declared like so: int i[10];
However, it would not work with dynamically allocated arrays: int *i = new int[10];
(sizeof( i ) would return the size of the pointer)

Besides, I think what he meant is how many items there are in the array, not the size of the array itself.
Quote:Original post by Endurion
Couldn't you get the size of an array (as in number of entries) by using sizeof( array ) / sizeof( array[0] ) ?
I've seen that snippet in MS statusbar pane filler code. This assumes that you have at least one entry of course.

Ahh yes, that's the code I was thinking of! However, trick is, I did something like this:
int * x = new int[50];int y = sizeof(x);std::cout << y;

And it printed 4. For your method to work, it would have to print, err, 200. I'm thinking the method you say doesn't work for dynamically allocated arrays, only ones like int x[50]; But I could always be wrong.

[EDIT]
Quote:Besides, I think what he meant is how many items there are in the array, not the size of the array itself.

If you devide the size of the array itself by the size of one element, you get the number of items in the array ;)
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote:
Quote:Besides, I think what he meant is how many items there are in the array, not the size of the array itself.

If you devide the size of the array itself by the size of one element, you get the number of items in the array ;)


No, the items that were inserted into the array, id est, an array is not always full. So you might have an array of size 10, but only 7 elements in it. And I think he wants to count the number of elements...
An array that isn't full? I thought the memory was all allocated when you declared it, either by 'int x[50]' or 'int * x = new int[50]'. You're telling me the memory is not allocated immediately? That's news to me..
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
I'm saying that the array may not be entirely filled with VALID elements.
I'll give an example, maybe it'll clear things up:
int main() {  int i[10];  for( int j = 0; j < 5; ++j )    i[j] = j;  cin.get();  return 0;}


Do you mean to tell me that i[5] through i[9] have values of interest?
Have you checked out AngleWyrm's Hat Random Container ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by jflanglois
I'm saying that the array may not be entirely filled with VALID elements.
I'll give an example, maybe it'll clear things up:
*** Source Snippet Removed ***

Do you mean to tell me that i[5] through i[9] have values of interest?

Fair enough, I didn't cotton on to that, as I tend to initialize my arrays on creation whenever possible. However, in this case, I would think it's probably a safe bet to say that Lenox will be doing that as well, initializing his arrays.

BTW, thanks for pointing that out to me.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!

This topic is closed to new replies.

Advertisement