Math for a random number

Paulsburbon

Member
Joined
Jan 21, 2007
Messages
7
Programming Experience
Beginner
Hello, I'm sorry if this should go into another specific forum. I looked and wasn't sure so decided to post it in here. I would like my users to be able to pick two numbers and have the program I'm writing randomly generate a number between the two. If you have a solution great if not maybe someone can point me in the right direction? Thanks!
 
Math for a random number.

Hello. Try this:
In the Form_Load event put this:-
VB.NET:
Dim i As Integer
For i = 1 To 12
Debug.WriteLine(rand(i))
Next
Then This:-
Function rand(ByVal carror AS Integer) As Integer
Dim obj As New System.Random(carrot)
Return obj.next(1,12)
End Function

That should? give random numbers between 1 and 12, but never 12.

See how you get along with that.
 
Hello again.
I forgot to say that at the very top of the code put:
VB.NET:
Imports System.Random
Also try having the event as a button_click and using Rnd.
and instead of 'Debug' use 'Console'.
Good to have variations.
 
Percentage?

And if I wanted to get random percentage between two percentages? Like say 5% and 25%? Or whatever numbers the user imputed?
 
Hello.
There is no special code for that except pure maths. Sorry I can't help you further with the maths but that would take me back a few years!
Work out a formula as, for instance:
x = someMaths then put it in code as this:

Dim x As Integer = someMaths
 
im not sure on this

use the rnd() function.

like in vb6,,

Call randomize

x = rnd()*1 ' this line randomly picks a number between 0 and 1

if x = 0 then MsgBox firstnum
if x = 1 then MsgBox secondnum
 
Use the .NET random number keywords, not the old VB6 keywords.

Here is the correct code. Copy and paste this code into a Console application:


Sub Main()
Dim num, hi, lo As Integer, entry As String
Dim randnum As Random = New Random()
Console.Write("Enter lowest number in random range: ")
entry = Console.ReadLine()
Integer.TryParse(entry, lo)
Console.Write("Enter highest number in random range: ")
entry = Console.ReadLine()
Integer.TryParse(entry, hi)
Console.WriteLine(vbCrLf & "10 random numbers within your range will display:")
For x As Integer = 1 To 10
num = randnum.Next(lo, hi + 1)
Console.Write(num & " ")
Next x
Console.WriteLine(vbCrLf)
End Sub
 
Hello. Try this:
In the Form_Load event put this:-
VB.NET:
Dim i As Integer
For i = 1 To 12
Debug.WriteLine(rand(i))
Next
Then This:-


That should? give random numbers between 1 and 12, but never 12.

See how you get along with that.

No no no. Do not re-initialise the random all the time! Do it once, then call random.Next(1, 12) every time you want a number
 
And if I wanted to get random percentage between two percentages? Like say 5% and 25%? Or whatever numbers the user imputed?

Erm.. What's the difference? How do you think a percentage differs from a normal number?

Here is a normal number:

5


Here is a percentage:

5%


So thats a normal number, with a % symbol after it.. Ergo, theres no difference. Get your random number, and when you print it, add a % symbol.
 
Hello, I'm sorry if this should go into another specific forum. I looked and wasn't sure so decided to post it in here.

This is the general discussion for Visual Studio - the text editor thing you type code into. If you had a question like "How can I change from inserting tabs to inserting spaces, when I press tab?" it would go in here.

If you had a question like "How do I get the current time?" or "How do I get a random number?" it would go in the VISUAL BASIC gen diss section:

VS.NET General Discussion -> STUDIO (the editor)
VB.NET General Discussion -> BASIC (the language)

;)
 
Back
Top