How do you use Delphi to generate any N random numbers between 0 and 15 that don t repeat?

Updated on technology 2024-02-09
10 answers
  1. Anonymous users2024-02-05

    Random numbers in pascal.

    Random numbers refer to a series of numbers that theoretically have no rules to follow, have an equal probability of each number appearing within a specified range, and cannot be used to follow the previous number. The basic principle of a general random number generator is: first, initialize a random seed, the initial value of which can be an arbitrary integer; Every time a random number is obtained, a special operation is performed based on the random seed, a random number is obtained and returned, and then some operation is performed on the random seed to change the value of the random seed.

    In this way, many relatively random numbers can be generated, but a random seed with the same initial value will produce the exact same random sequence.

    Pascal's system unit provides two subroutines related to random numbers: randomize and random.

    The randomize process is used to initialize a random seed, the initial value of which depends on the current system clock.

    The random function is used to get a random number.

    It has two forms of calling:

    random, which returns a random real number between 0 and 1 (not including 1);

    random(n) returns a random integer between 0 and n (excluding n), where n is a word integer.

    In addition, the identifier of the random seed variable in the system unit is randseed, which you can also change manually. Random numbers can be used in the Informatics Olympiad to randomize algorithms such as search, exhaustion, etc., to optimize their performance, and can also be used to select key numbers in the quicksort so that the worst-case scenario of the quicksort algorithm does not have a fixed corresponding number series. If you want a program that uses random numbers to have a constant output for the same input data, you can set randseed to a fixed value.

    random(10000)

    After taking out the number immediately, lz only needs to write a function to convert the number into a character, and fill in the zero in front of it to determine whether the character already exists.

    ps: How to judge depends on how you save, exist.

  2. Anonymous users2024-02-04

    1. The problem with the previous 0 is because (assuming that you are now entering 5, that is, num is 5), the length of the array is 5, but the random number value is random(5+1), that is, the random value is from 0-5, a total of 6 numbers, then naturally one cannot be taken.

    2. When changing to random(num), for k:=0 to i do, I personally feel that it should be for k:=0 to i-1 do

    3. There are many simple methods for random number algorithms, which are much simpler than goto, such as the most common pseudo-algorithms.

    a.Create an array x[n] and assign a value to each element (e.g. a random number between 0-n), then x[0]=0, x[1]=1 to x[n]=n;

    b.Starting from x[0] to x[n], swap any number between the element and x[0-n] at random.

    If it's delphi2010 or above, there's something like tdictionary.

  3. Anonymous users2024-02-03

    You take the 6 numbers from 0 to 5 every time, and only save 5 of them to display, there must be a number that does not have.

    It's like there's no 5 in the 2nd time.

    Why use goto, continue is fine.

  4. Anonymous users2024-02-02

    It seems that the program is generally in an endless loop. It is required that the num entered should not be less than 2, otherwise the program will make an error.

  5. Anonymous users2024-02-01

    Just add the judgment, and if you already have this number, take it again. Until you find a non-duplicate.

  6. Anonymous users2024-01-31

    Set the array to 0 when initializing. Since you for k:=0 to i do this place is pushed back one bit in the loop, when it is random to 0, it is always checked that 0 already exists, making it impossible for 0 to happen the first time.

    This place should be changed to for k:=0 to i-1 do

    The reason for the endless loop when changing to l:x:=random(num) is the same as before, which is for k:=0 to i do. The loop lasts, leaving only 0 unassigned, and all the time 0 is detected.

  7. Anonymous users2024-01-30

    Hello, I'm here to help you solve your problem:

    According to your needs, I have optimized and improved according to your **, and the actual test has passed!

    I'll upload the entire test source file!

    In fact, you can also make this ** into an independent function, so that it can be reused!

  8. Anonymous users2024-01-29

    Actually, it's too complicated to not use so many loops. You can refer to this post.

  9. Anonymous users2024-01-28

    1、randseed;"Seeds" that occur at any time. If no value is assigned, then the resulting random sequence is the same for each execution.

    So the value must be different for each call.

    2、randomize;Initialize the random number generator with the current machine time as the "seed".

    3. random() starts from the random number initialized by the "seed" and starts to generate a random number sequence But random() in delphi produces a pseudorandom number, that is, the random number generated by random() is the same in the two runs of the program. Run randomize first, and then random is the real random number, i.e., randomize to initialize the random number generator.

    random()

    Example: Generate random numbers between 20-500 randomize;

    20+random(500-30+1);

  10. Anonymous users2024-01-27

    Use this function to function randomrange(const afrom, ato: integer): integer;

Related questions
14 answers2024-02-09

function getsqlserverlist(strings:tstrings):boolean;

The function is implemented as follows: >>>More

9 answers2024-02-09

3 ways. 1. After changing the name, click File Replacement, browse to the file location and click Replace, and then save. >>>More

17 answers2024-02-09

Step 1 Method: Start by double-clicking to open PhotoshopCS4 >>>More

12 answers2024-02-09

Click "Start" - "Run" in the bottom left corner of the desktop, and then type "DXDIAG" Enter, which will launch the DirectX diagnostic tool, and you can see the current version number to the right of the "DirectX Version" entry in the lower part of the "System" tab. >>>More

8 answers2024-02-09

Because you're superimposing, it should be n+(n-1)+

So sum(n-1) should be called. >>>More