Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Aman

Fill Array with 'different' random numbers?

This topic is 6109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey thanks for taking the time to read this! 8o) I've been going at it for about 14hrs so its probably right before my eyes and Ill get it after a nap but just in case I figured I'd ask. (not 14hrs on this prob) I need to fill an array with 6 different random numbers from 0 to MAXNUMBER but none of the numbers can repeat in the array 834759 is ok (all different), 834549 is not ok (two 4's) I went round and round trying while, do while, for, else etc. Any help is appreciated, Thanks, Andrew some snips of code with my bad code removed #define MAXNUMBER 10 #define getrandom(min,max) ((rand()%(int)(((max)+1)-(min)))+(min)) int RandNumber[6]; // Randmom Number Array (RNA) // blah blah code stuff here.....skipped to relevent srand( (unsigned)time( NULL ) ); // Seeds the random for(int cnt=0; cnt<6; cnt++) // Fill the RNA with #'s { RandNumber[cnt]= getrandom(0, MAXNUMBER); } // This is just a straight fill but I need all Diferent numbers! Edited by - aman on November 22, 2001 1:05:04 PM

Share this post


Link to post
Share on other sites
Advertisement
Another do my homework for me post?

  
for (i = 0; i<6; i++)
do
{
a=[i]RandomNumber
} while ( hasRepeatedDigits (a[i]);


How do you check for repeated digits? There are lots of ways. You could for example, convert it to a string with itoa, and extract char''s from the string. Here is one way to get a digit mathematically:
  

int digit (int number, int pos)
{
if (number < pow (10, pos-1))
return -1; //This is a leading zero

return (number / ( (int)(pow(10, pos-1) ) ) % 10
}


So once you have the digits, just run though all of them and take a count. I leave the rest for you to figure out, so you can earn your grade.

Share this post


Link to post
Share on other sites
I think he mean 8,3,4,7,5,9 as 6 separate numbers with MAX_NUMBER = 10, not digits.

One way to go, in C++ would be to use a std::set, which will automatically reject duplicates.

  
std::set<int> myInts;
while( myInt.size() < 6 )
myInts.insert( getRandom(0, MAXNUMBER) );


Share this post


Link to post
Share on other sites
oh, well then that is really easy. Everytime you make a new random number, just check it against the existing numbers, and if it equals an existing number then reject it and make another one.
  
for (i = 0; i<6; i++)
do
{
a[i]=RandomNumber
} while ( isAnElement of (a[i], a);


its really easy to check if a number is already in the array. Just initialize the array to some non valid number and check vs all elements (the inefficent but conceptually simple way) or check element i against all elements from 0 to i-1.

Share this post


Link to post
Share on other sites
Hi, this code generates a number and then it loops trough the array where all previously generated numbers are stored, to check if the newly generated number has already been generated. Remember MAX_NUMBER >= NUMBERS, otherwise you'll get an infinite loop. Hopefully this can give you an idea how to solve this.

Good luck,
Nick


  
#define NUMBERS 5
#define MAX_NUMBER 10


int numbers[NUMBERS];


void main()
{
int n, i, j, f;

srand(time(NULL));

for (i = 0; i<NUMBERS; i++)
{
do
{
n = rand() % MAX_NUMBER; // generate a number


f = 0;
for (j = 0; j<i; j++)
if (numbers[j]==n)
f = 1; // number has been generated before

}
while (f);

numbers[i] = n;
}
}



Edit: forgot about the html

Edited by - nickm on November 22, 2001 1:56:20 PM

Share this post


Link to post
Share on other sites
invective - Another do my homework for me post? Hee been
outa school for 14+ years now... Ah if only 8o)

nickm - Worked like a charm...it was very simalr to what
i was trying except i was dumping outa loop here...

if (numbers[j]==n)f=1;

with

if (numbers[j]==n)f=0;

DOH!!!! hee guess the mind just fogs after staring at the
screen for that many hours!!!!!


Hey THANKS to everyone for answering!!!!!

Andrew

Share this post


Link to post
Share on other sites
A potentially more efficient method would be to generate a list of unique numbers, (eg. "1, 2, 3, 4... n") and then shuffling them randomly. A simple shuffle algorithm is

for each element of the array
swap it with another randomly chosen element

Or use the STL''s ''random_shuffle'' algorithm.

Share this post


Link to post
Share on other sites
Yup, gotta back-up Kylotan there. There is a probability that the code that recursively checks the array will never complete. Of course that probability is zero--but if you had a bigger problem, like an array of 600 numbers between 1 and 1000, this would take a really long time.

I''d just create a sequence of the numbers 0-9, do a std::random_shuffle, and take the first six numbers. Guaranteed constant time to do that algorithm.

Share this post


Link to post
Share on other sites
I've got a question about shuffle implementation. I've seen two potentially different algorithms presented for shuffling, and I'm wondering if they are equivalent or not...

Here's a presentation of each, using java:
    
public static int [] shuffledArray1(int size)
{
int [] result = new int[size];
for (int i = 0; i < size; i++) result[i] = i;

java.util.Random r = new java.util.Random();

for (int i = size - 1; i >= 0; i--)
{
int j = r.nextInt(size);

int temp = result[i];
result[i] = result[j];
result[j] = temp;
}

return result;
}

public static int [] shuffledArray2(int size)
{
int [] result = new int[size];
for (int i = 0; i < size; i++) result[i] = i;

java.util.Random r = new java.util.Random();

for (int i = size - 1; i >= 0; i--)
{
int j = r.nextInt(i);

int temp = result[i];
result[i] = result[j];
result[j] = temp;
}

return result;
}


The difference is that the first one swaps with a random element in the whole array. The second swaps with a random element in the part of the array that hasn't been iterated through yet.

Obviously they won't produce the same output, for any given random number seed. But will they produce equally well shuffled results?

Edited by - c_wraith on November 24, 2001 7:50:31 PM

Share this post


Link to post
Share on other sites
c_wraith

Or something like this, its all the same as long as you swap each number at least once.

  
public static int [] shuffledArray1(int size)
{
int [] result = new int[size];
for (int i = 0; i < size; i++) result[i] = i;

java.util.Random r = new java.util.Random();

for (int i = size - 1; i >= 0; i--)
{
int j = r.nextInt(r.nextInt(size));

int temp = result[i];
result[i] = result[j];
result[j] = temp;
}

return result;
}


Here is a very interesting link on this topic.

http://csrc.nist.gov/rng/

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!