Join 136,116 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!
will give a pseudo random number 0 to 52 inclusive. You can use time() to see the generator so each time you run you get a different sequence.
thx!~~ My problem that is i would like to Generate out 10 number .. between 0 and 52. Then I would like to make sure the generated out is (not) e.g. 0,1,2,3,4,5,6,7,8,9 or 10,11,12,13,14,15,16,17,18,19 etc/
I would like to make sure the radom number don't less or more than the coming up number.
if you need to check that a number does not occure more than once you can put the numbers into an array - get the next random number check thru the array if it is not a duplicate add it to the array otherwise ignore it.
Say I had a deck of cards and I wanted to shuffle them. Well lets say I put the numbers 0-51 into a 52 element array (cards[]). Then I loop though the array (using 'count' as a counter) selecting a random number (rnd) between 0 and 51, now I swap(cards[count], cards[count]). I may choose to loop though the deck of cards more than once to get a good shuffle.
The array now contains the numbers 0-52 arranged in a fairly unpredicatable manner. This method may not be the most secure for online gamming (though you would be surprized at how faulty some of those systems are), but it will make for an ok game of solitare.
The above is a cheap version of the Fisher-Yates or Knuth shuffle. There are other and even some better algorithms, but this is the model.
I'm trying to generate 10 radom number and i should make sure the number don't duplicate. but in my program, the upper one, it's still have opportunity to generate our duplicate number..~
Would any body can give me some hint ? Many thanks!!!
may i ask that. do i need to define the "swap".... as a int or something?
or i need to include some .h file ?
because i run this program , it has some error on swap .. Thanks so much!!!
swap is a function. It will swap the values for two of the array elements integers. You could write your own swap routine, use one of the ones listed in the snippets or just add #include <algorithm> to your code. (it is really just easy to just write a quick swap routine, and if this is for a class teachers don't ussualy like it if you use STL solutions).
As for your last post, my previous answer still holds.
Your code suffers from the problem that it does not check the entire array, it just makes sure that the last random number does not equal the current one. You would need to check the entire array:
CODE
<psuedo code> int Counter; int i; for (Counter = 0; Counter<Max; Counter++) { BadValue = true; while (BadValue) { value = GetRandom(1 to 52); BadValue=false; for (i=0; i<Counter;i++) { if (Array[i]==value) BadValue=true; break; } } }
Of course there are better ways to do this, this is just an example. The Knuth version is really a good algorithm.
thx !! I'm now can use the following code to gen. out 52 number without duplicate ~! but now i would to apply the these rand number into the GUI - cards so that it can make my cards radom out in GUI... I'm just have some idea to add a
-- but i don't know how to apply these random number to gen out the photo in GUI.... thx!! --------------------------------------------------------------------------------------------------------------------------
CODE
#include <iostream> #include <cstdlib> // for srand and rand #include <ctime> // for time using namespace std;
int main() { int card[52]; // array of cards; int n; // number of cards to deal srand(time(0)); // initialize seed "randomly"
for (int i=0; i<52; i++) { card[i] = i; // fill the array in order }
while (cin >> n) { //--- Shuffle elements by randomly exchanging each with one other. for (int i=0; i<(52-1); i++) { int r = i + (rand() % (52-i)); // Random remaining position. int temp = card[i]; card[i] = card[r]; card[r] = temp; }
//--- Print first n cards as ints. for (int c=0; c<n; c++) { cout << card[c] << " "; // Just print number } cout << endl; }