|
-
Feb 27th, 2006, 01:51 PM
#1
Thread Starter
Member
[RESOLVED] Random Number
Im making a number game and to do it I need the form to spit out any number between 1 and 100. I was able to make it spit out 70, but everytime the form loads, its always 70. Can someone show me a way to do it so that its a different number every time I start the form?
Thanks
-
Feb 27th, 2006, 01:56 PM
#2
Re: Random Number
Use Randomize
VB Code:
Dim iValue As Integer
Randomize
iValue = Int((100 * Rnd) + 1)
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 27th, 2006, 01:56 PM
#3
Thread Starter
Member
Re: Random Number
Does it have to be Integer or can it be Double? Whats the difference?
-
Feb 27th, 2006, 02:04 PM
#4
Re: Random Number
 Originally Posted by StudNewf
Does it have to be Integer or can it be Double? Whats the difference?
It can be double too, but then what exactly do you want it to do. Are you trying to generate random numbers between 1 to 100 with decimal places. If that is the case then you can use
VB Code:
Dim iValue As Double
Randomize
iValue = (100 * Rnd) + 1
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 27th, 2006, 02:07 PM
#5
Re: Random Number
VB Code:
Public Function RandomRange(ByVal RangeStart As Int32, ByVal RangeEnd As Int32) As Int32
Return New System.Random().Next(RangeStart, RangeEnd+1)
End Function
I converted it form C# in my head so hopefully it's right
-
Feb 27th, 2006, 02:12 PM
#6
Re: Random Number
Oh is it VB.NET. 
Till now I have been thinking it is VB 6.0. In .NET it is simple
VB Code:
Dim rndNumber As New Random
MessageBox.Show(rndNumber.Next(1, 100).ToString)
Ignore my previous two posts. I was under impression that this thread was posted in Classic VB forum. Sorry about that.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 28th, 2006, 08:51 AM
#7
Fanatic Member
Re: Random Number
In .NET 2005 it can be done like this
VB Code:
label.text = CStr(Int(Rnd() *100))
-
Feb 28th, 2006, 09:04 AM
#8
Re: Random Number
 Originally Posted by jre1229
In .NET 2005 it can be done like this
VB Code:
label.text = CStr(Int(Rnd() *100))
That isn't a vb05 feature, it is legacy... comes from vb6, still supported in all version of .net thus far.
the .net way is the above post, using the Random class.
-
Feb 28th, 2006, 01:41 PM
#9
Fanatic Member
Re: Random Number
Sorry, Never used VB6.. Just stating a way that I know is possible in VB 2005.
 Originally Posted by Phill64
That isn't a vb05 feature, it is legacy... comes from vb6, still supported in all version of .net thus far.
the .net way is the above post, using the Random class.
-
Mar 2nd, 2006, 10:28 AM
#10
Thread Starter
Member
Re: Random Number
Thanks everybody, I think I got it down pat now. I really appreciate your help.
Tahnks
-
Mar 2nd, 2006, 11:15 AM
#11
Re: Random Number
please don't forget to mark the thread resolved if you've gotten the answer.
-
Mar 3rd, 2006, 12:17 AM
#12
Thread Starter
Member
Re: Random Number
How do I mark it resolved?
-
Mar 3rd, 2006, 12:18 AM
#13
Thread Starter
Member
Re: [RESOLVED] Random Number
Nevermind I did it. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|