Home > Back-end >  How to make the function of the random number will not be repeated
How to make the function of the random number will not be repeated

Time:10-20

#include
#include
Void main ()
{int I;
Unsigned seed;
Printf (" do input seed: ");
The scanf (" % u ", & amp; Seed);
Srand (seed);
for(i=0; i<10; I++)
Printf (" % 3 d, "1 + rand () % 100);
printf("\n");
}

CodePudding user response:

There is not repeated random number? Have to assume that the scope of

CodePudding user response:

Use a random number or real clock do random seed,

CodePudding user response:

Code already use srand buy random seed, guarantee the random seed is different, the random number sequence is different, but there are in the same sequence number repeat is normal,

CodePudding user response:

The original poster is 10 times or more times in random values are all different.

CodePudding user response:

Before I wanted to do a similar approach is to list, 100, and each inside put a value, then generate a random number % 100, remove the corresponding number of position, then remove from the link, generating random Numbers % 99... This cycle of 100 times.

CodePudding user response:

Randmize ();
Random (10000) % 100;
I usually do

CodePudding user response:

Usually produce any random number easier to repeat... If you want to produce don't repeat random number... Process there than in the process of repeatedly and appear inefficient...
So you must use efficient card stacking ~ a given range

CodePudding user response:

 # include & lt; stdlib.h> 
#include
Const int NUM_MAX=100;/* range given */
Void main ()
{
Int val [NUM_MAX];
Int I, r;
/* starting set a random number generator */
Randomize ();
For (I=0; i /* to shuffle method of random exchange the values in the array... Have not repeat random number */
For (I=0; i R=random (NUM_MAX);
/* * no temporary stock exchange method/
Val [I] +=val [r].
Val [r]=val [I] - val [r].
Val [I] -=val [r].
}
For (I=0; i <10; I++) printf (" % 3 d ", val [I]);
printf("\n");
system("pause");/* please press any key to continue... */
}

CodePudding user response:

 # include & lt; stdlib.h> 
#include
Const int NUM_MAX=100;/* range given */
Void main ()
{
Int val [NUM_MAX];
Int I, r;
Unsigned seed;
/* set virtual random seed */
Printf (" do input seed: ");
The scanf (" % u ", & amp; Seed);
Srand (seed);
For (I=0; i /* to shuffle method of random exchange the values in the array... Have not repeat random number */
For (I=0; i R=random (NUM_MAX);
/* * no temporary stock exchange method/
Val [I] +=val [r].
Val [r]=val [I] - val [r].
Val [I] -=val [r].
}
For (I=0; i <10; I++) printf (" % 3 d ", val [I]);
printf("\n");
system("pause");/* please press any key to continue... */
}

CodePudding user response:

Only within a certain range not repeat it,

CodePudding user response:

Or upset a number column,

CodePudding user response:

Only within a certain range is not repeated

CodePudding user response:

The building Lord asked questions is very common in real life! Is a typical poker scrub,
It is very simple and basic theory of probability problem, first to write out a mathematical formula,

CodePudding user response:

Int __fastcall Pub_GetRand (float MIN, float MAX)//MIN, MAX asked for you
random number of upper and lower limits{
 

Float Max;
Max=RAND_MAX;
Srand ((unsigned) time (NULL));//random timing begins
Return (int) (rand () * (MAX - MIN) + MIN/MAX);
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

CodePudding user response:

Not to repeat random Numbers generated m 1, first, to generate a value of 0 to an array of n - 1
2, a random array subscript, put the subscript value exchange, with the first element
3, from the second element to the last element to randomly select the subscript, exchange, and the second element
4, has been to m times, in front of the array to match the idea m element is not repeated random number,

CodePudding user response:

The top 9 f

CodePudding user response:

First put your number in a List

1. Generate a random number range is the List. The Size () - 1.
2. Each produces a random number, List the number of deleted

Repeat 1, 2 are ok
  • Related