| | |
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.
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.
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
Solved Threads: 0
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:
as you can see this is the long-hand way... is there a better way to assign the "rand()%9;" command?
peace,
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:
C++ Syntax (Toggle Plain Text)
slots[1][1] = rand()%9; slots[1][2] = rand()%9; slots[1][3] = rand()%9; slots[2][1] = rand()%9; slots[2][2] = rand()%9; slots[2][3] = rand()%9; slots[3][1] = rand()%9; slots[3][2] = rand()%9; slots[3][3] = rand()%9;
as you can see this is the long-hand way... is there a better way to assign the "rand()%9;" command?
peace,
•
•
Join Date: Oct 2005
Posts: 8
Reputation:
Solved Threads: 0
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:
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:
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
}
}
}
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
}
}
}
pokerponcho
•
•
Join Date: Feb 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by 1o0oBhP
one way is to track what numbers are generated and save them in an array for instance. then check back to see if it is already generated.... IMHO if you have a range of over 50 then the numbers are going to still be random and its unlikely to get repeats. you could always get two random numbers in succession and multiply / add / whatever so that the combined numbers vary a bit more.
put names in an array, generate random numbers from 0 to the size of the array - 1. make sure of no repeats and then store the numbers in a separate array so you might have:
"Bill"
"Bob"
"James"
"Chris"
"Thomas"
so you would generate numbers from 0 to 4 and might get:
3
1
4
0
2
in a separate array. Then copy across from the string array the string at the index specified by the second array. Then it becomes:
"Chris"
"Bob"
"Thomas"
"Bill"
"James"
Hope this helps
i ll b glad.
Last edited by ~s.o.s~; Mar 13th, 2007 at 1:42 pm. Reason: Keep it on site.
•
•
•
•
Originally Posted by evilsilver
I use Borland aswell (i like it personally) as for the compiler it is just the play button (labeled as run) and when you say you click on the icon and it dissapears right away, are you talking about the Borland program itself, or the program you are trying to run? if it is the program it is because the computer is executing the program then seeing that there is nothing else to do closes itself. if you #include <conio> you can use the getch(); command at the end of your program which will keep the window open until you press anykey then it will close.
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:
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <cstdio> #include <ctime> #include <string> #include <vector> using namespace std; const int NULA = 0; vector< string > names; template< typename _T > inline void shuffle( _T begining, _T ending ) { int size = ending - begining; for( _T it = begining; it < ending; ++it ) swap( *(it), *(begining + (int)rand % size ) ); } int main( void ) { int n; char buff[256]; srand( (unsigned)time( 0 ) ); scanf( "%d", &n ); names.reserve( n ); for( int i = 0; i < n; ++i ) { scanf( "\n%[^\n]s", buff ); names.push_back( buff ); } shuffle( names.begin(), names.end() ); for( int i = 0; i < n; ++i ) printf( "%s\n", names[i].c_str() ); return NULA; }
Last edited by brahle; Mar 3rd, 2006 at 7:32 am. Reason: Some errorss...
Revenage is a dish best served cold.
50|2|2Y 4 |34|) 3|\|6|_|5|-| 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.
"...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.
Last edited by venomlash; Oct 15th, 2006 at 10:59 pm. Reason: forgot quote
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Reorder random numbers (C++)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: File read problem
- Next Thread: passing linked lists through functions??
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ cdialogbar char class classes code coding compile console conversion convert count delete deploy desktop developer directshow dissertations dll double-linkedlist download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loan loop looping loops map math matrix memory multiple news node number online output pagerank pointer problem program programming project python random read recursion recursive reference risk rpg string strings superclass temperature template test text text-file tree tutorial url validator variable vector video win32 windows winsock wordfrequency wxwidgets





