Form1.text1.text=1Private Sub Form_Load()
Text1.Text = Form1.Text1.Text
Me.AutoRedraw = True
For j = 5 To 8 + 5 - 1
Print Format(Rnd * (Val(Text1.Text) - (-2) + 1) + (-2), "0.00") &"%" 'Rnd * (最大值-最小值+1)) + 最小值
Next j
j = j + 1
End Sub怎么让这段代码每次窗口开出来都是固定的显示随机
比如第一次是显示的 0.98 0.9 0.7
下次窗口开出还是显示这个
Text1.Text = Form1.Text1.Text
Me.AutoRedraw = True
For j = 5 To 8 + 5 - 1
Print Format(Rnd * (Val(Text1.Text) - (-2) + 1) + (-2), "0.00") &"%" 'Rnd * (最大值-最小值+1)) + 最小值
Next j
j = j + 1
End Sub怎么让这段代码每次窗口开出来都是固定的显示随机
比如第一次是显示的 0.98 0.9 0.7
下次窗口开出还是显示这个
如果是要保存,然后显示,直接写到txt文件得了.
Text1.Text = Form1.Text1.Text
Me.AutoRedraw = True
For j = 5 To 8 + 5 - 1
Print Format(Rnd(-2) * (Val(Text1.Text) - (-2) + 1) + (-2), "0.00") &"%" 'Rnd * (最大值-最小值+1)) + 最小值
Next j
j = j + 1
End Sub
rnd(number), 给number一个负值,得到重复的随机数序列:
dim i as long
rnd -2
for i=1 to 10
debug.? int(rnd * 10);
next
debug.?
Text1.Text = Form1.Text1.Text
Me.AutoRedraw = True
For j = 5 To 8 + 5 - 1
Randomize
Print Format(Rnd * (Val(Text1.Text) - (-2) + 1) + (-2), "0.00") & "%" 'Rnd * (最大值-最小值+1)) + 最小值
Next j
j = j + 1
End Sub
打开程序-----》打开窗体-----》得到随机数-------》关闭窗体(不退出程序)-----再打开窗体还是跟之前得到的随机数一样-------》关闭程序
在你进入程序并获取随机数的时候要使用 Randomize
否则每次打开程序获取的随机数会相同
比如
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 10
Debug.Print Int(Rnd * 100 + 1)
Next i
Debug.Print "***************"
End Sub的结果将相同
71
54
58
29
31
78
2
77
82
71
***************
71
54
58
29
31
78
2
77
82
71
***************
Dim Fname$, RandStr$, i&, ProgStart As Boolean
Private Sub Form_Load()
Fname = "c:\test.txt"
If Dir(Fname) = "" Then
RandStr = ""
For i = 1 To 5
RandStr = RandStr & Format(CStr((Int(Rnd * 99) + 1) / 100), "0.00") & " "
Next i
RandStr = Trim(RandStr)
Open Fname For Output As #1 '将RandStr的内容写入TXT文件里面
Print #1, RandStr
Close #1
Else
Open Fname For Input As #1 '将TXT文件里面的随机测试项中的内容导入变量RandStr
RandStr = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
End If
Text1.Text = RandStr
Command1.Caption = "进窗体2"
ProgStart = True
End SubPrivate Sub Form_Unload(Cancel As Integer)
If Not ProgStart Then End
End SubPrivate Sub Command1_Click()
Form2.Show
ProgStart = True
Unload Me
End Sub'代码 2 Form2Private Sub Form_Unload(Cancel As Integer)
Form1.Show
End Sub
Text1.Text = Form1.Text1.Text
x = Rnd(-1)
Me.AutoRedraw = True
For j = 5 To 8 + 5 - 1
Print Format(Rnd * (Val(Text1.Text) - (-2) + 1) + (-2), "0.00") & "%" 'Rnd * (最大值-最小值+1)) + 最小值
Next j
j = j + 1
End Sub
楼主是要同一个窗体每次new出来时,load下的随机结果是一样....
我8楼思路楼主试了没?只要在循环随机前加一行:rnd n 'n小于0!
试过了,出来的随机数居然是一样的,我不是需要这样的重复,
打个比方:
第一次在FORM1上点击按钮 出来FORM2的窗口,FORM_LOAD里加载了0-2之间的随机数 1.9-----1.8----0.2----0.1
关闭FORM2(但是FORM1还是开着的)第二次在FORM1上点击按钮 出来FORM2的窗口,FORM_LOAD里还是加载了0-2之间的随机数 1.9-----1.8----0.2----0.1不是所有的随机数都一样(现在变成了0.2----0.2-----0.2---0.2(打个比方))
---------------------------------------------------------------------
这个结果只能说你代码有问题我试了,结果是:
---------------------------------------
打个比方:
第一次在FORM1上点击按钮 出来FORM2的窗口,FORM_LOAD里加载了0-2之间的随机数 1.9-----1.8----0.2----0.1
关闭FORM2(但是FORM1还是开着的)第二次在FORM1上点击按钮 出来FORM2的窗口,FORM_LOAD里还是加载了0-2之间的随机数 1.9-----1.8----0.2----0.1
-------------------------------------
'Form1:
Option ExplicitPrivate Sub Command1_Click()
Dim frm As Form
Set frm = New Form2
frm.Show
Set frm = Nothing
End Sub'Form2
Option ExplicitPrivate Sub Form_Load()
Me.AutoRedraw = True
Dim i As Long
Rnd -10
For i = 1 To 10
Print Rnd; 100
Next
End Sub
初始化随机数生成器。语法Randomize [number]可选的 number 参数是 Variant 或任何有效的数值表达式。说明Randomize 用 number 将 Rnd 函数的随机数生成器初始化,该随机数生成器给 number 一个新的种子值。如果省略 number,则用系统计时器返回的值作为新的种子值。如果没有使用 Randomize,则(无参数的)Rnd 函数使用第一次调用 Rnd 函数的种子值。注意 若想得到重复的随机数序列,在使用具有数值参数的 Randomize 之前直接调用具有负参数值的 Rnd。使用具有同样 number 值的 Randomize 是不会得到重复的随机数序列的。
c:\Microsoft SDK\src\crt\rand.c
/***
*rand.c - random number generator
*
* Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>#ifndef _MT
static long holdrand = 1L;
#endif /* _MT *//***
*void srand(seed) - seed the random number generator
*
*Purpose:
* Seeds the random number generator with the int given. Adapted from the
* BASIC random number generator.
*
*Entry:
* unsigned seed - seed to seed rand # generator with
*
*Exit:
* None.
*
*Exceptions:
*
*******************************************************************************/void __cdecl srand (
unsigned int seed
)
{
#ifdef _MT _getptd()->_holdrand = (unsigned long)seed;#else /* _MT */
holdrand = (long)seed;
#endif /* _MT */
}
/***
*int rand() - returns a random number
*
*Purpose:
* returns a pseudo-random number 0 through 32767.
*
*Entry:
* None.
*
*Exit:
* Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/int __cdecl rand (
void
)
{
#ifdef _MT _ptiddata ptd = _getptd(); return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );#else /* _MT */
return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
#endif /* _MT */
}
Cls
Dim i As Long
Rnd -10
For i = 1 To 10
Print Rnd; 100
Next
End Sub