Randomization

In the attached file, I have a simple script with an array with 6 elements each with a random value below 52. The problem is that every time I run the script I get the same numbers in the array. I want it to assign it new values every time I run it. How do I do that?
[267 byte] By [wildmagefantasy] at [2007-11-20 8:19:08]
# 1 Re: Randomization
Use srand() to seed the random sequence

Mike
MikeAThon at 2007-11-10 22:31:23 >
# 2 Re: Randomization
Use srand() to seed the random sequence
And if you REALLY want "new values" - use different seeds :)
VladimirF at 2007-11-10 22:32:18 >
# 3 Re: Randomization
In the attached file, I have a simple script with an array with 6 elements each with a random value below 52. The problem is that every time I run the script I get the same numbers in the array. I want it to assign it new values every time I run it. How do I do that?

Best to use the rand_s() as this will use the OS to produce true random numbers and there is no need to mess with seeding.
hoxsiew at 2007-11-10 22:33:16 >
# 4 Re: Randomization
That type of looping is inefficient, and could possibly run forever.

Better to make a list of the valid numbers, pick a random one from the list and remove it from the list. This way you are guarenteed to get unique numbers in a finite time.
TheCPUWizard at 2007-11-10 22:34:22 >
# 5 Re: Randomization
What are you trying to do with com[i] > 51?
GCDEF at 2007-11-10 22:35:26 >
# 6 Re: Randomization
That type of looping is inefficient, and could possibly run forever.

Better to make a list of the valid numbers, pick a random one from the list and remove it from the list. This way you are guarenteed to get unique numbers in a finite time.
Well, i assume you could simply make a list of numbers through an array, that's easy, but I'm not sure how I would randomly select a number from that list and then delete that number. Can I still use the rand() function? Because it doesn't seem like rand() can have any arguments.

What are you trying to do with com[i] > 51?
I needed the randomized numbers to be from 0 to 51. It was much easier to use %100 for the remainder on the randomizer than to figure out how to get numbers from 0-51 with a remainder, so that was to ensure that the number finally picked was below 51. That part works fine.
wildmagefantasy at 2007-11-10 22:36:25 >
# 7 Re: Randomization
I needed the randomized numbers to be from 0 to 51. It was much easier to use %100 for the remainder on the randomizer than to figure out how to get numbers from 0-51 with a remainder, so that was to ensure that the number finally picked was below 51. That part works fine.

Do you want 0 - 51 or below 51. Why not just use %51 or %52? Working and good aren't always the same.
GCDEF at 2007-11-10 22:37:23 >
# 8 Re: Randomization
Well, i assume you could simply make a list of numbers through an array, that's easy, but I'm not sure how I would randomly select a number from that list and then delete that number. Can I still use the rand() function? Because it doesn't seem like rand() can have any arguments.


As your list shortens, use the modulus operator to reduce the number of available choices to match the size of the list.

This is a C++ forum, so if you're coding C++ you could use random_shuffle on a vector of numbers from 0 - 51 and use the first 6 also.
GCDEF at 2007-11-10 22:38:31 >
# 9 Re: Randomization
Well, at the moment I'm trying to write a script that randomizes 6 numbers from 1-52. I wrote the script up using do-while statements and rand() and it works wonderfully, except that sometimes I get doubles on the numbers. So I added a part to the while statement that I thought would fix that, but it hasn't, and I dont know why (this is the version attached). Someone suggested that the loop is inefficient because it could possibly go on forever and to make a list of appropiate numbers, randomly select a number, then remove that number from the list. I know I could make a simply list of numbers with an array, but how do I randomly select a value from a list and then remove the number from that list?
wildmagefantasy at 2007-11-10 22:39:28 >
# 10 Re: Randomization
What GCDEFs suggested was that you create an STL-vector containing the numbers 1 - 52, call random_shuffle and then pick them one by one from front of vector (discarding the used elements)

From MSDN help:

// alg_random_shuffle.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

int main( ) {
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1, Iter2;

int i;
for ( i = 1 ; i <= 9 ; i++ )
v1.push_back( i );

random_shuffle( v1.begin( ), v1.end( ) );
cout << "The original version of vector v1 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// Shuffled once
random_shuffle( v1.begin( ), v1.end( ));
push_heap( v1.begin( ), v1.end( ) );
cout << "Vector v1 after one shuffle is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// Shuffled again
random_shuffle( v1.begin( ), v1.end( ));
push_heap( v1.begin( ), v1.end( ) );
cout << "Vector v1 after another shuffle is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
}
S_M_A at 2007-11-10 22:40:24 >
# 11 Re: Randomization
The attached script randomizes 6 numbers. My problem is that for my purposes none of the numbers can be the same, so I programmed in a do-while statement that I thought would prevent it, but upon compiling it still occasionally comes up with doubles. I honestly can't figure out why the do-while statement isn't working. Help?

I think you're probably looking for the shuffling algorithm. I'm guessing that 52 is in reference to the number of cards in a standard poker deck (minus jokers).

The Knuth Shuffle is pretty fast and should do what you want. You basically shuffle your array of 52 cards, then pick 6 (or however many you want) from the first 6 in the array. The algorithm is essentially:

void KnuthShuffle(int* pArr)
{
int rand;
for(int i=51;i>=0;i--)
{
rand=GenRand(0,i);
swap(pArr[i], pArr[rand]);
}
}

Where GenRand(a,b) is generates a random integer from a to b and swap() swaps two array elements.

You can search for "Knuth Shuffle" for more information.
hoxsiew at 2007-11-10 22:41:27 >
# 12 Re: Randomization
Best to use the rand_s() How is it better?
... this will use the OS ...You should say "Windows XP or better"
... to produce true random numbers..."Pseudo-random". Nothing is really "true random"...
... there is no need to mess with seeding.There also is no possibility to repeat the same pseudo-random sequence if needed.
VladimirF at 2007-11-10 22:42:25 >
# 13 Re: Randomization
One more thing:
If anyone cares - rand_s() takes 30 times (!) longer than rand().
VladimirF at 2007-11-10 22:43:36 >
# 14 Re: Randomization
The knuth shiffle could be especially well suited, especially if you need to "draw" additional cards. But I am hesitant to agree the "52" shuffles is sufficient to generate a true random deck. There is still a reasonable chance of a card being left (as opposed to returned to) its original position.
TheCPUWizard at 2007-11-10 22:44:36 >
# 15 Re: Randomization
this probly doesnt answer your question but just because you get doubles that only reenforces the fac that the numbers are random. for instance if you ask anybody to pick 5 random number out of a range of 1-10 then will probly say something like...

7 5 8 2 4

this is not truly random because they chose (whether intentional or unintentional) to pick different numbers, where as true random numbers may look like this:

5 5 3 8 1

so in essence tru random number sets do have doubles....and quite often
ne0n82 at 2007-11-10 22:45:29 >
# 16 Re: Randomization
And if you REALLY want "new values" - use different seeds :)

You can use the current time as the seed, which will ensure for all practical purposes as different for different instances of the application.

srand( (unsigned)time(NULL) );

Regards,
Pravin.
Pravin Kumar at 2007-11-10 22:46:38 >
# 17 Re: Randomization
[ merged ]
cilu at 2007-11-10 22:47:35 >
# 18 Re: Randomization
I think you're probably looking for the shuffling algorithm. I'm guessing that 52 is in reference to the number of cards in a standard poker deck (minus jokers).

The Knuth Shuffle is pretty fast and should do what you want. You basically shuffle your array of 52 cards, then pick 6 (or however many you want) from the first 6 in the array. The algorithm is essentially:

void KnuthShuffle(int* pArr)
{
int rand;
for(int i=51;i>=0;i--)
{
rand=GenRand(0,i);
swap(pArr[i], pArr[rand]);
}
}

Where GenRand(a,b) is generates a random integer from a to b and swap() swaps two array elements.

You can search for "Knuth Shuffle" for more information.

I looked it up... but I can't seem to figure out how to make it work. Is there a library I have to include for it? The compiler gives an error saying GenRand and swap are undeclared.

I'm probably showing myself as the most ignorant programmer out there... my preferred languages are web languages, like php, and while i'm a bit familiar with C, C++ i haven't worked with before.
wildmagefantasy at 2007-11-10 22:48:40 >
# 19 Re: Randomization
How is it better?

I'm making the assumption that wildmagefantasy is developing some sort of gaming algorithm. You certainly don't want the user to be able to determine the state of a PRNG and thus be able to predict the next card drawn.

You should say "Windows XP or better"

My docs say it is compatible back to Win98.

"Pseudo-random". Nothing is really "true random"...

See the documentation. It generates "cryptographically secure" random numbers. Certainly much better than rand().

There also is no possibility to repeat the same pseudo-random sequence if needed.
A good thing in this case I would imagine.
hoxsiew at 2007-11-10 22:49:32 >
# 20 Re: Randomization
I looked it up... but I can't seem to figure out how to make it work. Is there a library I have to include for it? The compiler gives an error saying GenRand and swap are undeclared.

I'm probably showing myself as the most ignorant programmer out there... my preferred languages are web languages, like php, and while i'm a bit familiar with C, C++ i haven't worked with before.

I mentioned at the bottom: "Where GenRand(a,b) is generates a random integer from a to b and swap() swaps two array elements". It is assumed that you implement these yourself. A web search for "knuth shuffle" should turn up links that show detailed implementations for your purposes.
hoxsiew at 2007-11-10 22:50:39 >
# 21 Re: Randomization
I mentioned at the bottom: "Where GenRand(a,b) is generates a random integer from a to b and swap() swaps two array elements". It is assumed that you implement these yourself. A web search for "knuth shuffle" should turn up links that show detailed implementations for your purposes.
Unfortunately a search for "knuth shuffle" came up with nothing but an explanation of what it does and the piece of code you gave me, but no instructions on how to implement it.
wildmagefantasy at 2007-11-10 22:51:40 >
# 22 Re: Randomization
Unfortunately a search for "knuth shuffle" came up with nothing but an explanation of what it does and the piece of code you gave me, but no instructions on how to implement it.
Look, it will help a LOT if you would state the purpose of your app.
If you are writing a secure "unbreakable" shuffle for Vegas-style casino - do your research. It has very little to do with programming, more - with math.
If you are trying to generate pseudo-random hand for your educational Poker game - don't bother. You simple rand() from the very first post will do just fine. Seed the randomizer with time() for "random" game, or with any number (so that you can repeat this specific game, for debugging or whatever).
Or, and you should read Donald Knuth's book "The Art of Computer Programming", just for fun.
VladimirF at 2007-11-10 22:52:43 >
# 23 Re: Randomization
Unfortunately a search for "knuth shuffle" came up with nothing but an explanation of what it does and the piece of code you gave me, but no instructions on how to implement it.

Try this (I haven't tested it):

int i,rand_i,tmp,deck[52];

srand(time(NULL));
//Fill the deck
for(i=0;i<52;i++){
deck[i]=i+1;
}

//shuffle the deck;
for(i=51;i>=0;i--){
rand_i=rand()%(i+1);
tmp=deck[rand_i];
deck[rand_i]=deck[i];
deck[i]=tmp;
}
hoxsiew at 2007-11-10 22:53:38 >