![]() |
| ||
| Re: C++ Random Numbers Hi! How to access random numbers one by one? Say calling random number 7 to be used by another function. |
| ||
| Re: C++ Random Numbers Hi Bob. I just wanted to know if random() could be included in ur list of random no generating functions as well. random works as a macro and a function: Macro: random( int num ) Function : int random( int num ) In both cases a random number between 0 and num-1 is generated. Also we use randomize() before we call rand()/random(). Any reason for that? Regards, Naveen. |
| ||
| Re: C++ Random Numbers Hello all, I'm new to the daniweb forums, as well as C++. I've decided to make a "slot machine" program... however the code for the random number is kind of messy. I've decided to use a [3][3] array to store the #'s, and this is how I currently have it set up: slots[1][1] = rand()%9; as you can see this is the long-hand way... is there a better way to assign the "rand()%9;" command? peace, |
| ||
| Re: C++ Random Numbers Quote:
for (int a=1; a<4; a++){ for(int b=1; b<4; b++){ slots[a][b] = rand()%9; OR slots[a][b]=( rand() *10 ) +1; //this assumes you wanted 1-10 } } |
| ||
| Re: C++ Random Numbers plzz i need some help!!how can i generate 100 unique integer random numbers in[1000, 9999] in array list, then how may i know how many of these fall in the range 1000-2500, 2501-5000, 5001-7500 and 7501-9999? |
| ||
| Re: C++ Random Numbers Generate 2 random numbers (call them arraySize1 and arraySize2) within a user-specified range of lower_size_bound to upper_size_bound, inclusive. Allocate 3 dynamic arrays of integers (called them array1, array2 and arrayCombo) sized arraySize1, arraySize2 and (arraySize1+arraySize2), respectively. Generate arraySize1 random numbers within a user-specified range of lower_value_bound to upper_value_bound, inclusive, and store each value (as soon as it is generated) into array1 using the StoreOrdered function (non-decreasing order) developed in class. Generate arraySize2 random numbers within the range of lower_value_bound to upper_value_bound, inclusive, and store each value (as soon as it is generated) into array2 using the StoreOrdered function (non-decreasing order) developed in class. Copy the values in array1 and array2 into arrayCombo using the CombineOrdered function developed in class. Output the values in arrayCombo in the form of a frequency plot for a user-specified range_width as illustrated below (the number of asterisks on each line is the number of values that fall within the indicated range) using a PlotArray function that you must develop: |
| ||
| Re: C++ Random Numbers This will shuffle integers aound anyway you like. So if you had an array that represented a deck of cards called "deck[]", you would call the function like this: shuffle(&deck, 52, 0, 0); This will give you a series of random cards from 0 to 51. The trick is to randomize the place to put the card in rather than to randomize the card. That's why I used pointers. It's not a matter of if you put an ace of spades, it's a matter of whether it should be the 4th or the 40th. void shuffle(int *array_ptr, int len, int def, int min) { int i = 0; int temp; for (i = min; i < len; ++i) { *(array_ptr + i) = def; // 'zero' the array } srand((unsigned)time(0)); // set an srand while (i < len) { // while not done shuffling temp = rand() % len + min; // get a random integer between min and len if (*(array_ptr + temp) == def) { // if nothing in the element yet // put i into element *(array_ptr + temp) = i; ++i; // incrament i } } } |
| ||
| Re: C++ Random Numbers Quote:
i ll b glad. |
| ||
| Re: C++ Random Numbers Quote:
BTW, why are you people using halfly C and halfly C++? Why do you use cin and cout and normal pointers? You see, cin and cout are much slower than scanf() and printf(), and when you have to input/output more than 10k of data, you see the difference. That is why I use scanf() and printf(). Normal pointers aren't used much in C++ because there is a templated conatiner vector that you can use to easily manipulate arrays. Elina: #include <algorithm> |
| ||
| Re: C++ Random Numbers Quote: "...my computer says it doesn't have i0stream and several other headers..." Did you maybe put in i0stream like you did in your message? Stupid question, but it only takes iostream. |
| All times are GMT -4. The time now is 2:24 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC