# Thread: How to pick random number from the list?

1. ## How to pick random number from the list?

I have list of area codes in the data table.
408,808,404,908,303,978,720,512.........

How do I pick different number every time i run the script?

2. ## Re: How to pick random number from the list?

Hello,

here is one way to do that:

1- you need to get the number of rows in your datatable:
numberofrows=Datatable.GetSheet(“Sheetname”).GetRo wCount

2- you need to Initialize the random-number generator.
Randomize()

3- By using below code you will get a random row from 1 to numberofrows

rowid=Int(numberofrows * Rnd()) + 1

4- You need to select the rowid cell by using:
datatable.SetCurrentRow(rowid)

5- get you value randomly from the table

Myvalue=datatable.Value(columnId,“Sheetname”) ' Columnid is the column where your values are stored. in your case I think ColumnId=area codes

the completed code is as follow:

numberofrows=Datatable.GetSheet(“Sheetname”).GetRo wCount
Randomize()
rowid=Int(numberofrows * Rnd()) + 1
datatable.SetCurrentRow(rowid)
datatable.Value(columnId,“Sheetname”)

Thanks.

3. ## Re: How to pick random number from the list?

Here's a code snippet that should work.
However, it could pick the same number twice in a row.
The important part is the Randomize and line that follows.

areacodes = Split("408,808,404,908,303,978,720,512",",")
mCount = UBound(areacodes) + 1
For I = 1 To 10

Randomize
index = Int(mCount * Rnd)

SelectedValue = areacodes(index)
Next

4. ## Re: How to pick random number from the list?

arr = Split("408,808,404,908,303,978,720,512",",")
arr( RandomNumber(1, Ubound(arr) + 1)) randomly picks up an area code for you each time

5. ## Re: How to pick random number from the list?

Intermittently i get this error..Re running the script works fine.Is there a way to suppress this error?

Subscript out of range: 'RandomNumber(...)'

6. ## Re: How to pick random number from the list?

arr = Split("408,808,404,908,303,978,720,512",",")

Randomize
index = Int((8 * Rnd) + 1)-1

msgbox arr(index)

But random function will not give you unique value always!
better you use simple loop and get values one by one

7. ## Re: How to pick random number from the list?

use data&amp;Time stamp which will be always unique.

8. ## Re: How to pick random number from the list?

What..and where should i use date time stamp?All I am doing is picking a random number from the list.

9. ## Re: How to pick random number from the list?

Chikki was thinking something else [img]/images/graemlins/smile.gif[/img] only looked at the last post and respond on the Unique value [img]/images/graemlins/smile.gif[/img]

You can use rajpes's code modified:

arr = Split("408,808,404,908,303,978,720,512",",")

Randomize
val = arr(Int((Ubound(arr) * Rnd) + 1)-1)

this will pick different numbers but again for unique you need to check if the number has been used before if then then remove that number form array first.

10. ## Re: How to pick random number from the list?

Still I get the subscript out of range error :&lt;
Any ideas?

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.