DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   C++ Random Numbers (http://www.daniweb.com/forums/thread1769.html)

paskal Feb 7th, 2005 4:16 am
Re: C++ Random Numbers
 
Hi!
How to access random numbers one by one? Say calling random number 7 to be used by another function.

Naveen Feb 7th, 2005 10:14 am
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.

Attendant Mar 14th, 2005 12:58 pm
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;
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,

winkland Mar 29th, 2005 1:16 am
Re: C++ Random Numbers
 
Quote:

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
}
}

perut_macho Sep 21st, 2005 1:15 am
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?

bucsoldier03 Oct 26th, 2005 10:23 pm
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:

pokerponcho Nov 23rd, 2005 4:37 pm
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
}
}
}

elina Feb 22nd, 2006 6:48 am
Re: C++ Random Numbers
 
Quote:

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.

brahle Mar 3rd, 2006 7:29 am
Re: C++ Random Numbers
 
Quote:

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:
#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;
}

venomlash Oct 15th, 2006 10:56 pm
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