There is a program that from memo1 (its content) displays in Edit1 , by means of pressing Button1 , randomly words, how to make sure that after the word is Button1 it no longer appears? Please explain everything in detail.

 var vorname : string; begin vorname := memo1.Lines[random(memo1.Lines.Count) + 1]; edit1.Text := vorname + ' '; end; 
  • 2
    The most obvious is, for example, delete a word from memo1 . - Nofate
  • one
    You will have to set forth in detail the course of discrete mathematics in order to explain that there is a linear congruential generator of pseudo-random numbers, what are its properties, and so on. - Vladimir Martyanov
  • It is necessary to re-initialize the generator (pleas) of random numbers. This is usually done by the current date (date as a number). And how it is done in delphi you already look for :) In Borland, initialization was invoked using the function randomize () - gil9red
  • 3
    @ gil9red Not true. Re-initialization of the RNG will not prevent the number from reappearing. - Kromster
  • @ Vladimir Martiyanov The question is much simpler and no course is needed. - Kromster

3 answers 3

The easiest way is to delete used words:

 var i: Integer; vorname: string; begin i := random(memo1.Lines.Count); // Даст значение от 0 до Count-1 vorname := memo1.Lines[i]; // Берем слово из соответствующей строки edit1.Text := vorname + ' '; memo1.Lines.Delete(i); // Удаляем строку со словом из "справочника" end; 
  • Thank you very much, what you need! - user202957

There are two ways not to repeat already used random values.

  1. Memorize all used values ​​in the array and for each new random value, check whether it is in the array or not.
  2. If you can use a not very large range of random numbers [0, D-1] , then:
    ~ set an array with values ​​from 0 to D-1 ;
    ~ A random number sensor must generate an index in this array;
    ~ after using a number from the array, write the last element of the array in its place, and reduce the range of generated indices D by one.

    A more versatile solution is TDictionary. http://keeper89.blogspot.com/2011/07/delphi-2.html (store a pair of "word" - "number of uses" or "line number" - "number of uses").

    We start a dictionary. Through random we take the word. We check its presence with the dictionary. When adding a word, add the word to the dictionary. And if the word in the dictionary is, then add +1 to the value.

    In this case, when all the words end we will be able to add words further, only instead of comparing with "0", we will compare with "1".