473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VB.NET: How do I randomize Words?

Hi, I'm new to VB.net. For a school project, I am trying to assign five variables to one label. And I want one of those variables to randomly show up when I hit the go button. Right now, a snip of my code looks like this:

Dim StrJob1 As String = "Teacher"
Dim StrJob2 As String = "House Maid"
Dim StrJob3 As String = "Lumber Jack"
Dim StrJob4 As String = "Doctor"
Dim StrJob5 As String = "Lawyer"
Dim StrJob6 As String = "Chef"



I hope this was clear... thanks in advanced.
Nov 24 '06 #1
2 3552
Use a random integer

Expand|Select|Wrap|Line Numbers
  1. Dim RandomWordNumber as Integer
  2.  
  3. Private Sub cmdShowWord_Click()
  4.  
  5.      'Generate a random number
  6.      RandomWordNumber = Int(6 * Rnd + 1)
  7.  
  8.      'See which number is for which word and change the label
  9.      If RandomWordNumber = 1 Then
  10.           lblWord.Caption = "Teacher"
  11.      End If
  12.  
Start with that and see what you can do, this code is copyright 2006 Peter Browne, GPL v2 Licensed - feel free to use but you must give credit as "Code Portion(s) (c) 2006 Peter Browne, peterggmss (thescripts.com), peterrbrowne@hotmail.com, GPL v2, Used Under License"
Nov 24 '06 #2
Seith
16
Expand|Select|Wrap|Line Numbers
  1. 'Required to tell the script to make a random number
  2. Public Function Rand(ByVal Low As Long, ByVal High As Long) As Long
  3.   Rand = Int((High - Low + 1) * Rnd) + Low
  4. End Function
  5.  
  6. Private Sub Command1_Click()
  7.  
  8. Dim random_number As Integer
  9. Randomize 'Declair Random
  10. random_number = Rand(1, 6) 'Generates random number from (Lowest, Highest)
  11.  
  12. Select Case random_number 'Sets the value that will change in the Cases
  13.     Case Is = 1
  14.         Label1.Caption = "Job One"
  15.     Case Is = 2
  16.         Label1.Caption = "Job Two"
  17.     Case Is = 3
  18.         Label1.Caption = "Job Three"
  19.     Case Is = 4
  20.         Label1.Caption = "Job Four"
  21.     Case Is = 5
  22.         Label1.Caption = "Job Five"
  23.     Case Is = 6
  24.         Label1.Caption = "Job Five"
  25. End Select
  26.  
  27. End Sub
  28.  
Requirements:
Command Button "command1"
Label "label1"

Action:
Clicking on command1 will make a random number from 1 to 6 and depending on the result (number) changes label1's caption to a specified (job title).

Customize:

Change the names of command1 and label1 through out the script
Change what the specified (job titles) are

Information:
Case = 1 is like saying "If {variable} = 1 then" it is used when a lot of different outcomes are possible, and it is how you will be taught to use them in the near future in your class ;)


P.S. You can't copyright common code that's used in everyday use of Visual Basic, only something you have created... if you attempted to patent this it would fail under being "not unique". Just so you know...
Nov 24 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fieldmedic | last post by:
I'm trying to determine the best way to randomize a set of array items. I'm thinking that I could use an arraylist and have it use the lower and upper bounds as the limits. Any suggestions? ...
3
by: Gaffer | last post by:
Hello Is there a way in which I can make certain parts of Html on my website random so that each viewer will see different material if they refresh the page or come back onto the website later?...
2
by: Rich | last post by:
Here is what I am trying for randomizing 2 numbers in the same subroutine so that they are not equal to each other: Dim j As Integer, k As Integer j = New System.Random().Next(0, 10) k = New...
4
by: Arnold | last post by:
Hi there, Here's the situation--there is a text field in a form in which students will key in data. On the keypress event, I'd like for different sounds to be played for each character typed,...
1
by: Badass Scotsman | last post by:
Hello, This code is supposed to generate a random string each run, however I have had it live on a few sites, and it seems to create repeat strings all over the place. ...
6
by: mrtaka79 | last post by:
Okay, first of all, I'm a complete noob, so go easy on me. I have this code that works perfectly for me. The only thing I want to add is to randomize the pictures/links that show up. Can anyone...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
1
by: VBSTUDENT | last post by:
I am just wondering if there is a way to randomize the aritmetic operators in code, I know how to randomize numbers but I am not sure if it is possible to randomize operators. Any help would be...
7
by: =?Utf-8?B?UGV0ZXIgTmV3bWFu?= | last post by:
I have inherited this vb6 code which i am trying to rewrite using vb.net 2005 I need to be able to get this working as we cannot issue out new end user software at the moment Public Function...
5
by: gggram2000 | last post by:
Hi, I'ved spent two full days trying to find a solution, I can randomize numbers between two ranges and it works fine, But my problem is when i want to randomize five numbers that I got. eg. I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.