10 Replies - 706 Views - Last Post: 30 June 2010 - 02:46 AM Rate Topic: -----

#1 S3lkies  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 248
  • Joined: 09-January 09

How to choose a question

Posted 28 June 2010 - 10:41 PM

Hey guys, I need to set up when a user presses a button it chooses a predetermined question (a selection of 4)

I have tried

Dim rand As New Random

rand.Next(0,5) 


However thats for numbers, I then tried

 Dim rand As New Random

rand.Next(w,z) 
and I keep getting errors.

So how can I set it that it chooses a letter between w and z.

and how would I set it up in the coding itself like would I make it
 W = question1 = inputbox("questionisinhere")
type thing?

Thanks alot for your help in advance.

This post has been edited by S3lkies: 28 June 2010 - 10:42 PM


Is This A Good Question/Topic? 1
  • +

Replies To: How to choose a question

#2 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: How to choose a question

Posted 28 June 2010 - 11:48 PM

This may help you.
Was This Post Helpful? -2
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to choose a question

Posted 29 June 2010 - 12:24 AM

Jack Eagles1 knock it off with the LMGTFY links dammit. Tons of people come here via a Google link only to get told to go use Google, so if you've not nothing useful or helpful to post in a thread just dont post. Stop trying to send our members back to Google!!
Was This Post Helpful? 4
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: How to choose a question

Posted 29 June 2010 - 09:45 AM

The first thing that comes to mind is to set up an array with the alphabet in it.
Choose a random number between the indexes where the letters reside...then retrieve the element at that random index number

There is probably another better way to do this but this is the first thing that comes to mind :)

-Frinny
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: How to choose a question

Posted 29 June 2010 - 10:36 AM

There are to sub tasks to this problem.

1. Get the string of characters eg. A to Z.

Note: This is done in vb10
Public Module Exts
  <Runtime.CompilerServices.Extension()>
  Public Function UpTo(ByVal FromC As Char, ByVal UpToC As Char) As String
    Dim f As Integer = CInt(Convert.ToUInt32(FromC))
    Dim u As Integer = CInt(Convert.ToUInt32(UpToC))
    Dim delta As Integer = u - f ' Which direction is the sequence going (Ascending, Descending or Static)
    delta = If(delta > 0, 1, If(delta < 0, -1, 1)) ' Almost Sign but 0 return 1 not 0
    ' Build the character sequence.
    Dim s As String = ""
    For c As Integer = CInt(If(f >= 0, f, u)) To CInt(If(f >= 0, u, f)) Step If(delta = 0, 1, delta)
      s &= Convert.ToChar(c)
    Next
    Return s
  End Function


Next is to create a random sequence from that selection.
  Dim rnd As New Random
  <Runtime.CompilerServices.Extension()>
  Public Function GetRnd(ByVal Source As String, ByVal Length As Integer) As String
    If Source Is Nothing Then Throw New ArgumentNullException("Source", "Must contain a string,")
    If Length <= 0 Then Throw New ArgumentException("Length must be a least one.", "Length")
    Dim s As String = ""
    For i = 1 To Length
      s &= Source(rnd.Next(0, Source.Length))
    Next
    Return s
  End Function
End Module



Since I used extension methods I can now simply write.

Module Module1

  Sub Main()
    Dim a = "W"c.UpTo("Z"c).GetRnd(1)
    ' the c mean Char Literal
  End Sub

End Module


Was This Post Helpful? 1
  • +
  • -

#6 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 178
  • View blog
  • Posts: 650
  • Joined: 03-February 10

Re: How to choose a question

Posted 29 June 2010 - 10:38 AM

 Dim rRandom As New Random(Now.Millisecond)

    Public Function GetRandomLetter(ByVal IsLowerCase As Boolean) As Char

        Dim ReturnValue As Char = Convert.ToChar(rRandom.Next(65, 91))

        If IsLowerCase Then
            ReturnValue = Char.ToLower(ReturnValue)
        End If
        Return ReturnValue

    End Function


This post has been edited by demausdauth: 29 June 2010 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: How to choose a question

Posted 29 June 2010 - 10:40 AM

You could use an Enum:
Module Module1
    Public Enum Letters
        W
        X
        Y
        Z
    End Enum

    Sub Main()
        Dim random As New Random()
        Dim l As Letters
        For i As Integer = 0 To 10
            l = CType(random.Next(0, 4), Letters)
            Console.WriteLine(l.ToString())
        Next
        Console.ReadKey()
    End Sub
End Module


Edit: Better advice has been posted while I was typing.

This post has been edited by insertAlias: 29 June 2010 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#8 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: How to choose a question

Posted 29 June 2010 - 10:54 AM

Yeah, PsychoCoder The reason why I posted the LMGTFLY link was because the first thing you get when you google it is this which is basically the answer. All he needed to do is COPY AND PASTE that code, and then generate the random string (there are instructions on the webpage), and then he's cool. There's no reason why he couldn't have done all that himself. He didn't need to post a topic here.

This is what he was looking for:


I found it in around 15 seconds.

Private Function RandomString(size As Integer, lowerCase As Boolean) As String
   Dim builder As New StringBuilder()
   Dim random As New Random()
   Dim ch As Char
   Dim i As Integer
   For i = 0 To size - 1
      ch = Convert.ToChar(Convert.ToInt32((26 * random.NextDouble() + 65)))
      builder.Append(ch)
   Next
   i If lowerCase Then
      Return builder.ToString().ToLower()
   End If
   Return builder.ToString()
End Function 'RandomString 


This post has been edited by Jack Eagles1: 29 June 2010 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#9 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 178
  • View blog
  • Posts: 650
  • Joined: 03-February 10

Re: How to choose a question

Posted 29 June 2010 - 10:54 AM

so my post was for letters A-Z so you would just have to change it to 87 as the low value to process W-Z.
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to choose a question

Posted 29 June 2010 - 01:21 PM

Jack Eagles1 it doesn't matter. People come here looking for help, not for someone to tell them to go check Google. I think you're missing the purpose of what we're here for and the service we're trying to offer. If you have any more to say it can be discussed via PM (though it will be a short discussion) and not hijacking this thread any longer
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: How to choose a question

Posted 30 June 2010 - 02:46 AM

View PostS3lkies, on 29 June 2010 - 05:41 AM, said:

Dim rand As New Random

rand.Next(w,z) 

The closest I can get is

 Sub Main()
    Dim rand As New Random
    Dim l = rand.NextChar("A"c, "Z"c)
    End Sub



Which is done with an extension method on the Random Class. It also uses the two previous extensions.
 
  <Runtime.CompilerServices.Extension()>
  Public Function NextChar(ByVal SourceRnd As Random,
                           ByVal FromC As Char,
                           ByVal UpToC As Char,
                           Optional ByVal Length As UInteger = 1) As String
    Return FromC.UpTo(UpToC).GetRnd(CInt(Length))
  End Function




Can't use Next for the function name as its a Keyword.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1