PDA

View Full Version : Random number generation



arrun
06-19-2008, 04:56 AM
Here I want draw 5 numbers at random from 1,2,3,4,..........,10 with replacement. Can anyone suggest me appropriate VBA code for that?

Regards,

Simon Lloyd
06-19-2008, 05:01 AM
Check out Andy Pope's site for the random lotto picker, you can specify the range of numbers to pick from the number of numbers to return and the number of iterations!

arrun
06-19-2008, 05:08 AM
Can you provide that link?

Simon Lloyd
06-19-2008, 05:12 AM
Errrrm....www.google.com

arrun
06-19-2008, 07:24 AM
I have searched Andy Pope's site here : http://www.andypope.info/vba.htm but perhaps there is nothing on that. But I feel that if you have information then you should provide that. I am not a professional VBA code writer, and that problem is a very small part of a particular assignment. If I get that then I could concentrate on my original problem.

Simon Lloyd
06-19-2008, 08:26 AM
I have searched Andy Pope's site here : http://www.andypope.info/vba.htm but perhaps there is nothing on that. Take a look at some of the examples and games
But I feel that if you have information then you should provide that. I am not a professional VBA code writer,If i have information then it is up to me whether i part with it or not regardless of your "status" you feel "I should" thats a little rude in my opinion as its a choice and it's all mine!
and that problem is a very small part of a particular assignment. If I get that then I could concentrate on my original problem.take a look at the forum rules we cannot help directly with assignments or homework only guide you in the right direction!

Sometimes a few minutes with google can supply you a whole host of examples for you to try to work with once you have tried manipulating something you have found and have no joy then post and we will help you come to a conclusion, to be honest if you're not willing to help yourself you can't expect us to!

Simon Lloyd
06-19-2008, 08:32 AM
A lottery picker does the same job as you require In Cell B2 enter =LotPick(B3,C3,D3), in cells B3, C3, D3 respectively enter 1, 49, 6 where 1 is the lowest number to pick from and 49 the largest and the amount of choices is 6.

Add a couple of buttons to the worksheet call one draw and one reset, now add this code to a standard module and assign the macros to their respective buttons.


Function LotPick(LEnd As Integer, TEnd As Integer, _
Amt As Integer) As String

Dim iArr As Variant
Dim i As Integer
Dim r As Integer
Dim temp As Integer

Application.Volatile

ReDim iArr(LEnd To TEnd)
For i = LEnd To TEnd
iArr(i) = i
Next i
For i = TEnd To LEnd + 1 Step -1
r = Int(Rnd() * (i - LEnd + 1)) + LEnd
temp = iArr(r)
iArr(r) = iArr(i)
iArr(i) = temp
Next i
For i = LEnd To LEnd + Amt - 1
LotPick = LotPick & " " & iArr(i)
Next i
LotPick = Trim(LotPick)
End Function

Sub Draw()
Dim iMyNumber As Long
For iMyNumber = 1 To 3
Calculate
Range("G65536").End(xlUp)(2).Value = Range("B2").Value
Next iMyNumber

End Sub

Sub Reset()
Sheets("Draw").Select
Columns("G:G").ClearContents
Range("G1").Select
End Sub
You should be able to work it out from there!