Question:
In C++, How can I generate a random number from interval [1000 , 9999]?
Joseph
2011-12-18 10:32:29 UTC
In C++, How can I generate a random number from interval [1000 , 9999] , but this number is distinct, I mean it doesn't have any same digits, like.. it can't be 8227 or 1821, all it's digits should be different like 8271?
and i also want this number to change through time.
Thanks in advance.
Three answers:
modulo_function
2011-12-18 15:24:43 UTC
McFate's second suggestion is good. I would implement it by defining an array for the 10 digits and when generating a new random digit, only use it in generating the 4 digit random if it's not already been used.



Something like this. I'll post this and then I'll test it...



int dig[10];

for( int k=0;k<10;k++ ) dig[k] = 0;

int count = 0;

int r;

int randOut = 0;

while( count<4 ) {

...r = rand()%10;

...if( count == 0 && r == 0 ) {/* do nothing */ } // leading, leftmost, digit cannot be 0

...else {

......dig[r]++;

......if( dig[r] == 1 ) { // can only use each digit once

.........randOut = 10*randOut+r;

.........count++;

......}

...}

}



Now I go test...



++add

Works great. Don't forget to rate answers.
McFate
2011-12-18 18:39:08 UTC
You can approach this in two ways:



(1) You can generate random numbers until you get one that matches your requirements (no repeated digits). This is easiest to code BUT kind of wasteful in that it might take many number generations to get one usable number.



(2) Generate the digits individually.

First digit randomly one of nine choices (1 - 9)

Second digit randomly one of nine choices (0 - 9, with first digit removed)

Third digit randomly one of eight choices (0 - 9, with first two digits removed)

Fourth digit randomly one of seven choices (0 - 9, with first three digits removed)



To generate a random number between 0 and N inclusive, use "rand() % (N+1)".



You can use "rand() % 9000 + 1000" to generate a random number between 1000 and 9999 if you use the first strategy.



You can use rand() % 9 to generate nine possibilities (0 through 8) which can be used as offsets into an array of valid digit choices if you use the second strategy. (You'd use %8 when there are only 8 choices, and %7 when there are only 7.)



for example:



int i, firstDigit, secondDigit, thirdDigit, digitChoices[9];

/* set up array */

for (i=0; i<9; ++i) digitChices[i] = i+1;

/* pick first digit */

firstDigit = digitChoices[ rand() % 9 ];



/* set up array for second digit */

for (i=0; i<9; ++i) digitChoices[i] = (i >= firstDigit) ? i+1 : i;

/* pick second digit */

secondDigit = digitChoices[ rand() % 9 ];



/* remove second digit from array */

for (int i=0; i<8; ++i)

if (digitChoices[i] >= secondDigit)

digitChoices[i] = digitChoices[i+1];

/* pick third digit */

thirdDigit = digitChoices[ rand() % 8 ];



"I want this number to change through time" just means you generate another one, I think. If you mean something else, you'll have to be more specific.



@M
anonymous
2011-12-18 22:32:31 UTC
//yahoo keeps truncating some of the statements when i paste, hope you can figure it out



include "stdafx.h"

#include "stdlib.h"

#include "time.h"

#include





#define DIGITS 4



int main(int argc,char *argv[])

{

int digitsSave[10] = {0,1,2,3,4,5,6,7,8,9};

int digitsWork[10];// = {0,1,2,3,4,5,6,7,8,9};

int index,rnd,result,loops = 25;



srand(time(NULL));

while(loops)

{

loops--;

memcpy(digitsWork,digitsSave,sizeof(digitsSave));

result = 0;

for(index = 0;index
{

rnd = rand()%(10-index);

if((index==0)&& (rnd == 0));//do nothing

else

{

result*=10;

result+=digitsWork[rnd];

for(;rnd<9-index;rnd++)

{

digitsWork[rnd]=digitsWork[rnd+1];

}

index++;



}



}

printf("%d\n",result);

}



getchar();

return 0;



}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...