Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random numbers in an array in VBScript

Posted on 2010-11-10
3
Medium Priority
?
1,166 Views
Last Modified: 2012-05-10
In vbscript, I need a function that will fill an array with random numbers but not repeating.  For instance, if I have 9 numbers, the numbers in the array need to be from 1 to 10.  Example:

Trial #1: 4,1,6,3,9,8,2,5,7
Trail #2: 9,3,1,4,5,7,2,6,8
...
0
Comment
Question by:onemorecoke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 2000 total points
ID: 34107603
I would use the dictionary object so that you can check if the number has already been added to the array.  Here is some sample code for you.  Now a slight warning - this is based of a random number generator.  It could "get stuck" running in the loop until it randomly generates a missing number.



Option Explicit
Dim dicNum, i, blnAdded, intNum, myvalue, strMsg

Set dicNum = CreateObject("Scripting.Dictionary")

For i = 0 to 9
	blnAdded = False
	Do until blnAdded
		intNum = Cint(Rnd()*10)
		intNum = Cint(Rnd()*10)
		If not dicNum.Exists(intNum) Then
			blnAdded = True
			dicNum.Add intNum, intNum
		End If
	Loop
Next

For each myvalue in dicNum.Items
	strMsg = strMsg & " : " & myvalue
Next

msgbox strMsg

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34107920
...or you can just FILL the array with 1 to 10. then SHUFFLE it by swapping random indices.
0
 
LVL 13

Expert Comment

by:Daz_1234
ID: 34110566
Interesting puzzle!

Here's my go.  Once it uses a number from 1 to 10, it removes that number so it cannot be chosen again.

Regards,
Daz.
Option Explicit

Dim arrTrial(10), strTrial, dicNums, i, arrNums, iNum

Randomize
Set dicNums = CreateObject("Scripting.Dictionary")

'# Load dictionary with 1 to 10
For i = 1 to 10
    dicNums(i) = i
Next

'# Load arrTrial array with random elements from dictionary, removing the element from the dictionary whenever it is used
Do
    i = dicNums.Count
    arrNums = dicNums.Keys
    iNum = Int(Rnd()*i + 1) - 1
    arrTrial(i - 1) = arrNums(iNum)
    dicNums.Remove(arrNums(iNum))
    If dicNums.Count <1 Then Exit Do
Loop




'#  Test the array

strTrial = Join(arrTrial, " ")

MsgBox "arrTrial = " & strTrial

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question