Solved

random test

Posted on 2006-11-03
17
165 Views
Last Modified: 2010-04-30
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))
------------------------------------------------------------------------











   
0
Comment
Question by:rmtogether
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
17 Comments
 
LVL 17

Expert Comment

by:CSecurity
ID: 17872742
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 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
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 17872744
Change at the line:
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 :)
0
 

Author Comment

by:rmtogether
ID: 17873456
hi, CSecurity

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
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 17

Expert Comment

by:CSecurity
ID: 17873527
Sorry for the code.
Replace this line:

 For i = 1 to 5
    Randomize


To

For i = 0 to 4

:)
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 17873530
Change For i = 1 to 5 to 0 to 4

It exists multiple times in code... Be careful
0
 

Author Comment

by:rmtogether
ID: 17873729
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
----------------------------------------------------------------------
0
 

Author Comment

by:rmtogether
ID: 17873806

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
0
 
LVL 10

Expert Comment

by:sakuya_su
ID: 17875402
because you stored the values into nums() rather thant prob(), the code you posted in no where was buf assigned any value? so ti will always be 0 xD
0
 

Author Comment

by:rmtogether
ID: 17875449

so what should I do in order to have random probability matrix. the sum in each row is equal to 1
0
 
LVL 17

Accepted Solution

by:
CSecurity earned 500 total points
ID: 17876833
I tested this code and works well:



 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
0
 

Author Comment

by:rmtogether
ID: 17877051

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
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 17877055
ok... as you like...
0
 

Author Comment

by:rmtogether
ID: 17877079
hi, CSecurity

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
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 17877089
You want in VB or C?
0
 

Author Comment

by:rmtogether
ID: 17877102

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.

0
 
LVL 17

Expert Comment

by:CSecurity
ID: 17877132
Open it in vb and give me link... I have not enough time for C now
0
 

Author Comment

by:rmtogether
ID: 17877344
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Send outlook email from VBS Script 2 66
VB script to continue despite error 2 77
Macro problems with Excel file 6 51
Modify Text File with Excel Macro 13 43
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question