Home > Back-end >  For random number between 000-999?
For random number between 000-999?

Time:09-29

My question is like this..

I added two label is respectively: label1, label2; There is a button btn1Click
Assuming that label1=300,
Label2=3,

Click btn1Click, can get the result,,,,

Label1=300, 300, which means application in number from 000-999, one thousand randomly selected 300 don't repeat number,
Label2=3, the three means: random three times, three times this results cannot be completely random,
Final results are written to the three random text, name of test1 respectively. TXT, test2. TXT, test3. TXT

Consult everybody the teacher, how to write the code...

Try to ask baidu before Posting.

CodePudding user response:

With a random number.
For I:=0 to do number - 1
Get a random number, to determine whether a pick up to the same as the previous, the same is to obtain
The last is a written document,
Thinking about is such, don't have time to help you write code,

CodePudding user response:

Thank you tip mathsfan teacher

CodePudding user response:

reference 1st floor mathsfan response:

o with random numberFor I:=0 to do number - 1
Get a random number, to determine whether a pick up to the same as the previous, the same is to obtain
The last is a written document,
Thinking about is such, don't have time to help you write code,


Because the generated random number repeat problem (from the range of less than 1000, 000-999, extract hundreds, repeat the probability is very high, so there should not be used for loop!

Use a while loop control points should be good. Get random number, didn't meet the requirements, will continue to generate.

CodePudding user response:

Use a while loop is better, take a random number of 0-1000, after to check a TStringList is there, not is in them, and the number 1.
Until the number added to 300, with a TStringList savetofile stored to TXT,
If you want to save 3 and not the same, I don't understand is what meaning is not the same, is it the same number but different order of the same, not completely the same number if required, need to be sorted each comparison, if determine three TStringList is not the same, then save one by one to TXT,

CodePudding user response:

reference 4 floor dongyonggan response:
use a while loop is better, take a random number of 0-1000, after to check a TStringList is there, not is in them, and the number 1.
Until the number added to 300, with a TStringList savetofile stored to TXT,
If you want to save 3 and not the same, I don't understand is what meaning is not the same, is it the same number but different order of the same, not completely the same number if required, need to be sorted each comparison, if determine three TStringList is not the same, then save one by one to TXT,


The teacher good.

Have the same number in a different order is the same...

So as you say,,, need to sort after each comparison.. Different output

CodePudding user response:

CodePudding user response:

"Online search" draw, it can avoid repeat number,

CodePudding user response:

You get 0-999 data in a List.
Then take a random later put the number of deleted from the List, then randomly from the List

CodePudding user response:

B: well... I found a card stacking of VBA.. Should draw algorithm...

Let me see...

For teachers to write code. So learn

CodePudding user response:

Difficult not
Will and it is not hard to

CodePudding user response:

To oneself knot stick?

CodePudding user response:

Procedure GetNum (ACount: Integer; AStr: TStrings);
Var
Arr: an Array of Integer;
I, iValue: Integer;
The begin
SetLength (Arr, 1000);
I:=0;
Randomize.
While I & lt; Do ACount - 1
The begin
IValue:=the Random (1000);
If Arr [iValue]=0 then
The begin
AStr. Add (IntToStr (iValue));
Inc (I);
end;
end;
SetLength (Arr, 0);
end;

Procedure TForm1. Button1Click (Sender: TObject);
Var
T: TStringList;
I, iCount iNum: Integer;
The begin
ICount:=300;//this number if you need can be random
INum:=3;//this number if you need can be random
T:=TStringList. Create;
For I:=1 to iNum do
The begin
Tc Lear.
GetNum (iCount, t);
T.S aveToFile (ExtractFilePath (Application. ExeName) + 'Text' + IntToStr (I) + '. TXT ");
end;
FreeAndNil (t);
end;

CodePudding user response:

Procedure GetNum (ACount: Integer; AStr: TStrings);
Var
Arr: an Array of Integer;
I, iValue: Integer;
The begin
SetLength (Arr, 1000);
I:=0;
Randomize.
While I & lt; Do ACount - 1
The begin
IValue:=the Random (1000);
If Arr [iValue]=0 then
The begin
Arr [iValue] :=iValue;//sorry, missing words
AStr. Add (IntToStr (iValue));
Inc (I);
end;
end;
SetLength (Arr, 0);
end;

CodePudding user response:

Thank you baidu_21144645 teacher... I test the

CodePudding user response:

The 0 first.. 999 added to the List, then the random draw out, take a delete one

CodePudding user response:

Procedure TForm1. Button1Click (Sender: TObject);
Var
I, nIdx: Integer;
LoadList: TStringList;
SGet: String;
The begin
LoadList:=TStringList. Create;
For I:=0 to 999 do LoadList. Add (IntToStr (I));

For I:=0 to 2 do begin//extract 3, then 0.. 2
NIdx:=the Random (LoadList. Count);
SGet:=LoadList. Strings [nIdx];
LoadList. Delete (nIdx);
end;
end;

SGet in the content not the same every time

CodePudding user response:

Take out remember release LoadList oh

CodePudding user response:

Generate the list, take that data will be removed after delete, it is certainly no longer take the same,
  • Related