C++ Random Numbers

Closed Thread

Join Date: Feb 2005
Posts: 1
Reputation: paskal is an unknown quantity at this point 
Solved Threads: 0
paskal paskal is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #11
Feb 7th, 2005
Hi!
How to access random numbers one by one? Say calling random number 7 to be used by another function.
Quick reply to this message  
Join Date: Jul 2004
Posts: 25
Reputation: Naveen is an unknown quantity at this point 
Solved Threads: 0
Naveen's Avatar
Naveen Naveen is offline Offline
Light Poster

Re: C++ Random Numbers

 
0
  #12
Feb 7th, 2005
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.
Quick reply to this message  
Join Date: Mar 2005
Posts: 1
Reputation: Attendant is an unknown quantity at this point 
Solved Threads: 0
Attendant Attendant is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #13
Mar 14th, 2005
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:

  1. slots[1][1] = rand()%9;
  2. slots[1][2] = rand()%9;
  3. slots[1][3] = rand()%9;
  4. slots[2][1] = rand()%9;
  5. slots[2][2] = rand()%9;
  6. slots[2][3] = rand()%9;
  7. slots[3][1] = rand()%9;
  8. slots[3][2] = rand()%9;
  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,
Quick reply to this message  
Join Date: Mar 2005
Posts: 1
Reputation: winkland is an unknown quantity at this point 
Solved Threads: 0
winkland winkland is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #14
Mar 29th, 2005
Originally Posted by Attendant
is there a better way to assign the "rand()%9;" command?

peace,
This may not be better, but it is shorter

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
}
}
Quick reply to this message  
Join Date: Sep 2005
Posts: 1
Reputation: perut_macho is an unknown quantity at this point 
Solved Threads: 0
perut_macho perut_macho is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #15
Sep 21st, 2005
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?
Quick reply to this message  
Join Date: Oct 2005
Posts: 8
Reputation: bucsoldier03 is an unknown quantity at this point 
Solved Threads: 0
bucsoldier03 bucsoldier03 is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #16
Oct 26th, 2005
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:
Quick reply to this message  
Join Date: Nov 2005
Posts: 10
Reputation: pokerponcho is an unknown quantity at this point 
Solved Threads: 0
pokerponcho's Avatar
pokerponcho pokerponcho is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #17
Nov 23rd, 2005
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
}
}
}
pokerponcho
Quick reply to this message  
Join Date: Feb 2006
Posts: 1
Reputation: elina is an unknown quantity at this point 
Solved Threads: 0
elina elina is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #18
Feb 22nd, 2006
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
would you please send me a c++ code of this, if you have because i tried but i can't get the right answer. my e-mail address: <snipped>

i ll b glad.
Last edited by ~s.o.s~; Mar 13th, 2007 at 1:42 pm. Reason: Keep it on site.
Quick reply to this message  
Join Date: Mar 2006
Posts: 18
Reputation: brahle is an unknown quantity at this point 
Solved Threads: 0
brahle's Avatar
brahle brahle is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #19
Mar 3rd, 2006
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.
I say djgpp is way better than Borland. Frist of all, djgpp is a Gnu software. Secondly, you can use much more memory than in Borland's compiler. In the end, in most of the competitions the djgpp is the default compiler.

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:
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7. const int NULA = 0;
  8. vector< string > names;
  9.  
  10. template< typename _T > inline void shuffle( _T begining, _T ending ) {
  11. int size = ending - begining;
  12. for( _T it = begining; it < ending; ++it )
  13. swap( *(it), *(begining + (int)rand % size ) );
  14. }
  15.  
  16. int main( void ) {
  17. int n;
  18. char buff[256];
  19. srand( (unsigned)time( 0 ) );
  20.  
  21. scanf( "%d", &n );
  22. names.reserve( n );
  23. for( int i = 0; i < n; ++i ) {
  24. scanf( "\n%[^\n]s", buff );
  25. names.push_back( buff );
  26. }
  27. shuffle( names.begin(), names.end() );
  28. for( int i = 0; i < n; ++i )
  29. printf( "%s\n", names[i].c_str() );
  30.  
  31. return NULA;
  32. }
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|-|
Quick reply to this message  
Join Date: Oct 2006
Posts: 142
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: C++ Random Numbers

 
0
  #20
Oct 15th, 2006
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.
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.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC