Delphi随机生成不重复的数使用Randomize和Random()并将结果保存到listbox1中

山东蓝鸟贵薪 2019-12-17 04:38:40
Delphi随机生成不重复的数使用Randomize和Random()并将结果保存到listbox1中

求个函数
...全文
487 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
山东蓝鸟贵薪 2019-12-21
  • 打赏
  • 举报
回复
谢谢赐教,太感动了


分已送上,请笑纳
天行归来 2019-12-21
  • 打赏
  • 举报
回复
引用 12 楼 山东蓝鸟贵薪 的回复:
[quote=引用 10 楼 天行归来 的回复:] 0=<Random(X)<X,自己调整下就可以了
function GetRandValue(const lb: TListBox; seed: integer): integer; begin Randomize(); while true do begin //无可用新的随机数(前提是seed不变情况下,否则有BUG) if lb.Items.Count=seed then begin result := -1; break; end; result := Random(seed); if lb.Items.IndexOf(IntToStr(result))>=0 then Continue; lb.Items.Add(IntToStr(result)); break; end; end; 请赐教修改那个地方[/quote]


function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
  Randomize();
  while true do
  begin
    //无可用新的随机数(前提是seed不变情况下,否则有BUG)
    if lb.Items.Count=seed then
    begin
      result := -1;
      break;
    end;

    result := Random(seed)+1;

    if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

    lb.Items.Add(IntToStr(result));
    break;
  end;
end;
山东蓝鸟贵薪 2019-12-21
  • 打赏
  • 举报
回复
引用 10 楼 天行归来 的回复:
0=<Random(X)<X,自己调整下就可以了




function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
Randomize();
while true do
begin
//无可用新的随机数(前提是seed不变情况下,否则有BUG)
if lb.Items.Count=seed then
begin
result := -1;
break;
end;

result := Random(seed);
if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

lb.Items.Add(IntToStr(result));
break;
end;
end;

请赐教修改那个地方
lovedeadyou 2019-12-20
  • 打赏
  • 举报
回复
这两个是组合使用的吧。
天行归来 2019-12-19
  • 打赏
  • 举报
回复
0=<Random(X)<X,自己调整下就可以了
山东蓝鸟贵薪 2019-12-19
  • 打赏
  • 举报
回复
引用 7 楼 天行归来 的回复:
[quote=引用 6 楼 天行归来 的回复:]


function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
Randomize();
while true do
begin
result := Random(seed);
if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

//无可用新的随机数(前提是seed不变情况下,否则有BUG)
if lb.Items.Count=seed then result := -1;
lb.Items.Add(IntToStr(result));
break;
end;
end;


调用
GetRandValue(ListBox1,100);


改进下:


function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
Randomize();
while true do
begin
//无可用新的随机数(前提是seed不变情况下,否则有BUG)
if lb.Items.Count=seed then
begin
result := -1;
break;
end;

result := Random(seed);
if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

lb.Items.Add(IntToStr(result));
break;
end;
end;
[/quote]
===========================================
还是有个 BUG
我的调试生成5个随机数
var
i :Integer ;
begin
i := 1 ;
ListBox2.Clear ;

while i<= 5 do
begin
GetRandValue(ListBox2,5);

i := i +1;
end;

Label13.Caption := '统计列表记录数为: ' + IntToStr(ListBox2.Count );
-----------------------------------------------------------------------------------
但是结果如下;


多出了一个 0,但是少一个 5
ooolinux 2019-12-18
  • 打赏
  • 举报
回复
random的参数应该是max?
天行归来 2019-12-18
  • 打赏
  • 举报
回复
引用 6 楼 天行归来 的回复:


function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
  Randomize();
  while true do
  begin
    result := Random(seed);
    if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

  //无可用新的随机数(前提是seed不变情况下,否则有BUG)
  if lb.Items.Count=seed then result := -1;
    lb.Items.Add(IntToStr(result));
    break;
  end;
end;
调用 GetRandValue(ListBox1,100);
改进下:


function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
  Randomize();
  while true do
  begin
    //无可用新的随机数(前提是seed不变情况下,否则有BUG)
    if lb.Items.Count=seed then
    begin
      result := -1;
      break;
    end;

    result := Random(seed);
    if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

    lb.Items.Add(IntToStr(result));
    break;
  end;
end;
天行归来 2019-12-18
  • 打赏
  • 举报
回复


function GetRandValue(const lb: TListBox; seed: integer): integer;
begin
  Randomize();
  while true do
  begin
    result := Random(seed);
    if lb.Items.IndexOf(IntToStr(result))>=0 then Continue;

    lb.Items.Add(IntToStr(result));
    break;
  end;
  //无可用新的随机数(前提是seed不变情况下,否则有BUG)
  if lb.Items.Count=seed then result := -1;
end;
调用 GetRandValue(ListBox1,100);
山东蓝鸟贵薪 2019-12-18
  • 打赏
  • 举报
回复
思路有些乱,帮写个 函数体 
秋天之落叶 2019-12-17
  • 打赏
  • 举报
回复
先生成一定量的顺序自然数,然后放入stringlist中,使用随机函数产生两个随机数,然后使用置换函数进行置换Exchange(x,y),循环次数可以自定,一般按照自然数的最高值循环,最后按照stringlist的顺序输出即可。
ooolinux 2019-12-17
  • 打赏
  • 举报
回复
每生成一个新随机数,在历史数组里搜索,如果搜到则重新生成,搜不到则将新随机数插入数组。
山东蓝鸟贵薪 2019-12-17
  • 打赏
  • 举报
回复
就是根据一个范围,比方说 100
Randomize和Random() 获取随机种子,并且不相同的
再将结果保存到listbox1中
  • 打赏
  • 举报
回复
不重复就不是随机,而是洗牌了,一个数组,初始化为顺序值,比如0~10000,然后洗牌,若干次随机交换两个元素,输出的时候按下标顺序输出即可

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧