Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String
Random Function and Walking Through a String Random Function and Walking Through a String
Random Function and Walking Through a String
Go Back  Xtreme Visual Basic Talk > > > Random Function and Walking Through a String


Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2009, 08:22 PM
BrynnFlynn BrynnFlynn is offline
Newcomer
 
Join Date: Oct 2008
Posts: 10
Default Random Function and Walking Through a String


Okay, so I'm in the middle of trying to translate a program from Python into Visual Basic so my users have a GUI. I have no prior experience in VB 2008, but I'm very comfortable in Python.

I have two major issues: I need to generate a random float (decimal number) between 0 and 1. I tried making a random function as you will see in the code below, but it's not working, it just gives me a 0 I believe.

On top of that, I need to walk through a string of substrings, from the start of the string to the end. The program takes in numbers separated by spaces (e.x., "12 32 54 34"), and then I believe separates them into a string of sub-strings, named bList ({12, 32, 54, 34}). However, the program isn't walking through the list, and instead outputs odd numbers.

I've included how I did it in Python, and what I have so far in Visual Basic. If someone could help me I would really appreciate it.

PYTHON CODE
Code:
    elif type(bList) == list:
        for i in range(len(bList)):
            baseBall = bList[n]
            for i in range(totBalls):
                if minSize != maxSize:
                    size = randrange(minSize, maxSize)
                elif minSize == maxSize:
                    size = minSize
                    
                xCoord = 2 * random() - 1
                yCoord = 2 * random() - 1
                zCoord = 2 * random() - 1

            n = n + 1

VB 2008 CODE
Code:
'A program to automatically generate paintballs for the Petz series of games.
'Written by Brynn from Brynn Bytes, 2009.
'Steal mah sheet, I keek your ***.


Public Class autoSpotGUI
    Dim totNum As New Integer
    Dim minSize As New Integer
    Dim maxSize As New Integer
    Dim fuzz As New Integer
    Dim texture As New Integer
    Dim tempBall As String
    Dim colorBall As New Integer
    Dim bList() As String
    Dim baseBall As New Integer
    Dim n As New Integer
    Dim fSize As New Integer
    Dim random As New Random
    Dim xCoord As New Integer
    Dim yCoord As New Integer
    Dim zCoord As New Integer

    Private Sub goButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goButton.Click
        n = 1
        colorBall = CInt(colorTextBox.Text)
        texture = CInt(textureBox.Text)
        fuzz = CInt(fuzzBox.Text)
        maxSize = CInt(maxSizeBox.Text)
        minSize = CInt(minSizeBox.Text)
        totNum = CInt(totNumBox.Text)
        tempBall = CStr(ballNumsBox.Text)
        bList = tempBall.Split(" ")

        For i = 0 To bList.Length
            baseBall = bList(n)
            For x = 0 To totNum
                If minSize <> maxSize Then
                    fSize = random.Next(minSize, maxSize)
                End If
                If minSize = maxSize Then
                    fSize = minSize
                End If

                xCoord = (2 * random.NextDouble) - 1
                yCoord = (2 * random.NextDouble) - 1
                zCoord = (2 * random.NextDouble) - 1

                outCodeBox.AppendText(baseBall & ",   " & xCoord & ",   " & vbCrLf)

            Next
            n = n + 1
        Next
    End Sub

End Class
Reply With Quote
  #2  
Old 02-20-2009, 08:24 AM
Iceplug's Avatar
IceplugRandom Function and Walking Through a String Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

To get a random double, you need to store the result in a double/single type. Storing it in an integer will turn your floating point number into an integer (no decimal point)

For the strings,
baseBall = bList(n)
You should convert the value in bList(n) to an integer using Integer.Parse().

The for loop should also go from 0 to bList.Count - 1
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String Random Function and Walking Through a String
Random Function and Walking Through a String
Random Function and Walking Through a String
 
Random Function and Walking Through a String
Random Function and Walking Through a String
 
-->