The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

hi,

I need have some data for my test. this is something I want to do

(1) generate 5 random numbers ( for example 3,5,3,6,9) and then calculate their probability like (3/26, 5/26, 3/26, 6/26, 9/26) then

(2) write this probabilities to a text file. the program should allow me do more than one test, for example if I need 3 test data. The program will give 3 rows in a text file like below

0.1 0.2 0.3 0.2 0.2

0.4 0.1 0.2 0.1 0.2

0.1 0.1 0.1 0.4 0.3

this is something I just write, but I don't know how to continue, and how to write to a text file. Could someone help me about this? thanks in advance

-------------------------------------------------------------------

Dim c As Integer

Dim min_num As Integer

Dim max_num As Integer

min_num = 1

max_num = 10

Randomize

c= min_num + Int(Rnd() * (max_num - min_num))

------------------------------------------------------------------------

I need have some data for my test. this is something I want to do

(1) generate 5 random numbers ( for example 3,5,3,6,9) and then calculate their probability like (3/26, 5/26, 3/26, 6/26, 9/26) then

(2) write this probabilities to a text file. the program should allow me do more than one test, for example if I need 3 test data. The program will give 3 rows in a text file like below

0.1 0.2 0.3 0.2 0.2

0.4 0.1 0.2 0.1 0.2

0.1 0.1 0.1 0.4 0.3

this is something I just write, but I don't know how to continue, and how to write to a text file. Could someone help me about this? thanks in advance

--------------------------

Dim c As Integer

Dim min_num As Integer

Dim max_num As Integer

min_num = 1

max_num = 10

Randomize

c= min_num + Int(Rnd() * (max_num - min_num))

--------------------------

Dim buf as string

Dim i as integer

Dim prob(0 to 4) as integer

Dim total as integer

Dim nums(0 to 4) as integer

Dim c As Integer

Dim min_num As Integer

Dim max_num As Integer

For t = 1 to X ' CHANGE X TO NUMBER OF TIMES YOU WANT TO DO RANDOMIZE TEST

min_num = 1

max_num = 10

For i = 1 to 5

Randomize

nums(i) = min_num + Int(Rnd() * (max_num - min_num))

total = total + nums(i)

next i

For i = 1 to 5

prob(i) = c / total

buf = buf & prob(i) + " "

next i

buf = buf & vbcrlf

next t

Open "C:\Result.txt" for binary as #1

Put#1, , buf

Close#1

For t = 1 to X -> X to number of times you want to do test. (lines you want to print)

in line:

Open "C:\Result.txt" -> change the filename to where you want to print result.

Just this :)

I have run time error in this line.

nums(i) = min_num + Int(Rnd() * (max_num - min_num))

the error is "subscript out of range". could you please help me about this?

thanks

I change Change For i = 1 to 5 to 0 to 4, but below

but there is a "type mismatch" error problem.

buf = buf & prob(i) + " " ----------------> type mismatch

-------------------code---

Dim t As Integer

Dim buf As String

Dim i As Integer

Dim prob(0 To 4) As Integer

Dim total As Integer

Dim nums(0 To 4) As Integer

Dim c As Integer

Dim min_num As Integer

Dim max_num As Integer

For t = 1 To 3 ' CHANGE X TO NUMBER OF TIMES YOU WANT TO DO RANDOMIZE TEST

min_num = 1

max_num = 10

For i = 0 To 4

Randomize

nums(i) = min_num + Int(Rnd() * (max_num - min_num))

total = total + nums(i)

Next i

For i = 0 To 4

prob(i) = c / total

buf = buf & prob(i) + " "

Next i

buf = buf & vbCrLf

Next t

Open "C:\Result.txt" For Binary As #1

Put #1, , buf

Close #1

--------------------------

I put "Str" in this line like,

buf = buf & Str(prob(i)) + " "

but my output always is 0 like below.. could you please help me about this?

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

thanks,

I take off the line

'If Len(Str(prob(i))) > 3 Then prob(i) = Left(Str(prob(i)), 3)

because I want to sum of each row equal to 1

I posted another question in C language. if you can help me implement in VB or C it would be great. if you want me open another topic please let me know. I really want to finish this program

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_22049608.html

thanks in advance

either C or VB is ok.

but if you can give me both will be great!! I will open the same topic in VB section.

i just open in VB section

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_22049704.html

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Dim t As Integer

Dim buf As String

Dim i As Integer

Dim prob(0 To 4) As Double

Dim total As Integer

Dim nums(0 To 4) As Integer

Dim c As Integer

Dim min_num As Integer

Dim max_num As Integer

Dim tmp As String

For t = 1 To 3 ' CHANGE X TO NUMBER OF TIMES YOU WANT TO DO RANDOMIZE TEST

min_num = 1

max_num = 10

For i = 0 To 4

Randomize

nums(i) = min_num + Int(Rnd() * (max_num - min_num))

total = total + nums(i)

Next i

For i = 0 To 4

prob(i) = nums(i) / total

If Len(Str(prob(i))) > 3 Then prob(i) = Left(Str(prob(i)), 3)

buf = buf & prob(i) & " "

Next i

buf = buf & vbCrLf

Next t

Open "C:\Result.txt" For Binary As #1

Put #1, , buf

Close #1