none
MasterMind RRS feed

  • Question

  • Hello

    I've just started learning how to code visual basic (.Net FrameWork) and i was wondering how can i create a random number between 0000 and 9999

    i've tried things like number = Int(Rnd() * 9998) for 4 lines of code 

    e.g.

    dim X1 as new random 

    x1 = Int(Rnd() * 9998) 

    i then want to divide it into 4 seperate digits e.g. if x1 is 9876 i want it to show 9,8,7,6 so that i can see if im correct for guessing a number e.g. if my guess was 9772, it'd say 9 was correct and so was 7 

    Is there a way to do that in .Net FrameWork?

    my current code is this

    Module Module1
        Sub Main()
            Dim Difficulty As String
            Console.WriteLine("Welcome to MasterMind")
            Console.WriteLine("Choose between Easy and Hard difficulty")

            Difficulty = Console.ReadLine
            While Difficulty <> "Easy" And Difficulty <> "easy" And Difficulty <> "Hard" And Difficulty <> "hard"
                Console.WriteLine("That's not a correct mode")
                Difficulty = Console.ReadLine
            End While
            If Difficulty = "Easy" Or Difficulty = "easy" Then
                Easy()
            ElseIf Difficulty = "Hard" Or Difficulty = "hard" Then
                Hard()
            End If

        End Sub

        Sub Easy()
            Dim RanNum(0, 1, 2, 3) As ArrayList
            Dim Guess As Integer
            Dim Tries As Integer
            RanNum = ?
            Console.WriteLine("You have chosen the easy difficulty")
            Console.WriteLine("You have to guess a 4 Digit number between 1000 and 9999")
            Console.WriteLine("Enter your guess")
            Guess = Console.ReadLine
            While Guess < 999 And Guess > 10000
                Console.WriteLine("Enter a number between 1000 and 9999")
                Guess = Console.ReadLine
            End While

        End Sub

        Sub Hard()
            Console.WriteLine("You have chosen the hard difficulty")
            Console.ReadKey()
        End Sub

    End Module



    Saturday, May 18, 2019 5:35 PM

All replies

  • Hi

    The Random number generator is much better than the old Rnd method now. You declare a New Random and use / reuse throughout your code as many times as needed.

    As below, you define the tange needed - such as 1000 to 10000, nut remember, in the method to use the Max needed +1 (it is an inclusive / exclusive type pair)

    RANDOM

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim rand As New Random
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' find a new random number between
    		'  1000 And 10000 NOTE second  
    		'  parameter is max required +1
    		Dim newnumber As Integer = rand.Next(1000, 10001)
    
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, May 18, 2019 6:14 PM
    • Proposed as answer by Alex Li-MSFT Wednesday, May 22, 2019 2:23 AM
    Saturday, May 18, 2019 6:12 PM
  • Hi,

    Do you resolve the issue? If you resolve the issue,  please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 2:25 AM