Random Letters

How do I get a Random Letter

I think I need to first get a Random number, then have the number = a Letter

i.e A=1, B=2 etc but how do I do this



Answer this question

Random Letters

  • Kiranvukkadala

    Reply to Whoisit

    You're creating an array of 16 letters. Do you need to check that the letters are not duplicated in the array

    If you do, how do you do it

    I ask because I want some random alphabets. It's a question of generating a series of numbers with no duplicates. One example is the numbers 1 to 26; another one is numbers 1 to 49 (lottery); and a third is taking a random number of questions from a list.

    I'm stuck on the duplication check. I guess it must be done by looping until you hit a number that isn't already in the array. So far I've come up with several infinite loops.

    Mongkut


  • vomjom

    Hi,

    Is your problem solved If no, can you be more clear on problem definition

    Thank you,
    Bhanu.



  • coenzyme77

    Any letter Will any English CAPITAL letter do

    Dim letter As String 'for any CAPITAL letter in English

    Randomize()

    ' Generate random value between 1 and 26

    Dim rndLetter As Integer = CInt(Int((26 * Rnd()) + 1))

    letter = ChrW(rndLetter + 65) 'A to Z ASCII 65 to 90

    --------------------------------------------------------------------------

    I want to randomize the whole English alphabet and ca't remember how to do it.

    Mongkut


  • dragon6158

    Thanks for ideas. That got me started, but, am I right, your code sample is for selecting six lottery numbers out of 49

    I want to rearrange the alphabet and have come up with a possible answer - though it may look a bit long.

    One button and one textbox:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim vNumberString As String = "" 'to hold the alphabet

    Dim vNumberArr(25) As Integer 'to hold random 1 to 26

    Dim vNumberTemp As Integer = 0 'to hold each temp random

    Dim i, j, k As Integer 'counters

    For i = 0 To 25 'generate all numbers between 1 and 26

    Randomize()

    For j = 0 To i 'to i or to 25

    'k loop produces a number

    For k = 0 To i 'to i or to 25

    Do Until vNumberTemp <> vNumberArr(k)

    'which it can't be on the first pass

    vNumberTemp = CInt(Int((26 * Rnd()) + 1))

    Loop

    Next k

    'j loop checks if the number is present already

    If vNumberArr(j) = vNumberTemp Then

    Do While vNumberTemp = vNumberArr(j)

    vNumberTemp = CInt(Int((26 * Rnd()) + 1))

    Loop

    End If

    Next j

    'put the number into the array

    vNumberArr(i) = vNumberTemp

    'convert numbers to letters A-Z

    vNumberString = vNumberString & ChrW(vNumberArr(i) + 64) & ","

    Next i

    'display comma-separated random alphabet

    TextBox1.Text = vNumberString

    End Sub

    The next question is how to compare two strings that ought to contain the same elements but in a different order.

    Mongkut


  • dschnare

    This might help:

    Dim str As String = ""

    Dim i, n As Integer

    Dim c As Char

    Dim rand As New Random

    For i = 0 To 9 ' Length of string required

    n = rand.Next(65, 65 + 26) ' A thru Z

    c = System.Convert.ToChar(n)

    str = str + c

    Next

    MessageBox.Show(str)

    It's perhaps not the best way of achieving this, but it works...

    HTH



  • John_W_F

    Try this, there is probabaly a better way, but this works.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim intNumber As Integer
    Dim arrNumber(0 To 5) As Integer
    Dim x, y As Integer
    '------------------------------------------------

    For x = 0 To 5
    Start:
    Randomize()
    intNumber = Int((49 * Rnd()) + 1) ' Random number 1 to 49
    For y = 0 To 5
    ' If number already been selected select another one
    If intNumber = arrNumber(y) Then
    GoTo Start
    End If
    Next y
    arrNumber(x) = intNumber
    Next x

    '----------------------------------------------------
    'Display non duplicated Numbers

    TextBox1.Text = (arrNumber(0))
    TextBox2.Text = (arrNumber(1))
    TextBox3.Text = (arrNumber(2))
    TextBox4.Text = (arrNumber(3))
    TextBox5.Text = (arrNumber(4))
    TextBox6.Text = (arrNumber(5))


    End Sub

    End Class

  • severo1980

    Well caught, I was just about to post up a comment on it.


  • RBone

    MISTAKE

    Sorry, the random letter example returns a number in the range 66 to 91; meaning no letter A and and a [ after Z.

    The random number is in the range 1 to 26, so add 64 not 65.

    Another way to do it ought to be

    anyNumber Mod 26 + 65

    Mongkut (after breakfast)


  • viru234

    Sorry about this but the Alert me to replies must have been down as I have tonight had a load of alerts.

    Here is what I am using and it works a treat, so thanks to all those contributed to this thread.

    For Letter = 0 To 16
    intLetter = CInt(Int((90 - 65 + 1) * Rnd() + 65)) 'GenerateRandom Letters
    arrLetter(Letter) = Chr(intLetter)
    Next

  • TimHiggison

    This is shorter

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim intLetter As Integer
    Dim arrNumber(0 To 25) As Integer
    Dim Number As Integer
    Dim i, y As Integer
    'Clear(Label)
    Label1.Text = ""
    '------------------------------------------------
    Randomize()
    For Number = 0 To 25
    Start:

    intLetter = CInt(Int((90 - 65 + 1) * Rnd() + 65)) 'GenerateRandom Letters
    For y = 0 To 25
    ' If number already been selected select another one
    If intLetter = arrNumber(y) Then
    GoTo Start
    End If
    Next y
    arrNumber(Number) = (intLetter)
    Next Number

    '----------------------------------------------------
    'Display non duplicated Letters i a list 1 = w, 2 = r, etc
    For i = 0 To 25
    Label1.Text = Label1.Text & (i + 1) & " = " & Chr(arrNumber(i)) & Chr(13)
    Next

    End Sub

    End Class

  • Random Letters