For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > April 2006 > Randomize question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Randomize question
Billy B

2006-04-28, 6:56 pm

I have the following code developed and it works fine. The number 125 in line
12 is the index number in the table. I have a two column Access table with
the w number and a word associated with that number. There are 25 words
per w and I want to randomly generate 25 words from the first five ws
using the w numbers 1, 2, 3, 4 and 5. Instead of using the index number I
want to use the w numbers in the randomize statement.

I am in instructor and this is the last thing I need to figure out to make
the testing program work. Thank you.

Private Sub GetWords()
Set rsTest = New clsRecSet
rsTest.rsSpell.Open strSQL
Dim intNum As Integer
Dim intRand As Integer
Dim intN As Integer
rsTest.rsSpell.MoveFirst
For intNum = 0 To 24
Here:
Randomize
'Need to randomize using numbers 1,2,3,4,5
intRand = Int((125 - 1) * Rnd + 1)
rsTest.rsSpell.Move intRand, adBookmarkFirst
If rsTest.rsSpell.Fields(2).Value = False Then
txtCorrAns(intNum) = rsTest.rsSpell.Fields(1)
rsTest.rsSpell.Fields(2).Value = True
Else
GoTo Here
End If
Next
End Sub
Larry Serflaten

2006-04-28, 6:56 pm


"Billy B" <BillyB@discussions.microsoft.com> wrote
> I have the following code developed and it works fine. The number 125 in line
> 12 is the index number in the table. I have a two column Access table with
> the w number and a word associated with that number. There are 25 words
> per w and I want to randomly generate 25 words from the first five ws
> using the w numbers 1, 2, 3, 4 and 5. Instead of using the index number I
> want to use the w numbers in the randomize statement.
>
> I am in instructor and this is the last thing I need to figure out to make
> the testing program work. Thank you.


It is not entirely clear what you want to happen. About the best I can gather
(from your words and code) is that you want 5 lists of 25 words from your
database of 125 words.

After a bit of thought, it may be you want to use words 1-25 for w one,
and 26-50 for w two, etc but still generate randomized lists. If that is
the case, you simply have to pick 1 number from 25 and add it to the
lower index value that is dependant on the current w (1, 26, 51, 76 ...)

But first, be aware, that Randomize is not a shake command as you
would think of shaking the dice, it is a seeding process which you should
do only once (at the start of your application).

Now for a bit of modification:

>
> Private Sub GetWords(ByVal WNumber As Long)

If WNumber < 1 or WNumber > 5 Then Err.Raise 5 <<< added
> Set rsTest = New clsRecSet
> rsTest.rsSpell.Open strSQL
> Dim intNum As Integer
> Dim intRand As Integer
> Dim intN As Integer
> rsTest.rsSpell.MoveFirst
> For intNum = 0 To 24
> 'Need to randomize using numbers 1,2,3,4,5 <<< Randomize removed
> intRand = Int(25 * Rnd) + (WNumber * 25 - 24) <<< changed
> rsTest.rsSpell.Move intRand, adBookmarkFirst
> If rsTest.rsSpell.Fields(2).Value = False Then
> txtCorrAns(intNum) = rsTest.rsSpell.Fields(1)
> rsTest.rsSpell.Fields(2).Value = True
> Else
> GoTo Here
> End If
> Next
> End Sub



Larry Serflaten

2006-04-28, 6:56 pm


"Larry Serflaten" <serflaten@usinternet.com> wrote[color=darkred]
>
> "Billy B" <BillyB@discussions.microsoft.com> wrote

A second look leads me to mention, it would be better to
adjust your query to select the entire list of words, and
use a second array to randomize them, rather than bouncing
back and forth to the DB checking to see if the next word
has already been picked.

HTH
LFS



Billy B

2006-04-28, 9:56 pm

Larry,

Sorry about any confusion with the way the question was written. You are
close but I need to get any 25 words from the 125 available (25 words*5
ws) not just 5 from each w.

I am not sure what you mean about a secondary array or how I would do that.
The randomizing does only happen when the program is started..

Thank you.


A couple things..I want to get

"Larry Serflaten" wrote:

>
> "Larry Serflaten" <serflaten@usinternet.com> wrote
>
> A second look leads me to mention, it would be better to
> adjust your query to select the entire list of words, and
> use a second array to randomize them, rather than bouncing
> back and forth to the DB checking to see if the next word
> has already been picked.
>
> HTH
> LFS
>
>
>
>

J French

2006-04-29, 3:56 am

On Fri, 28 Apr 2006 19:50:01 -0700, =?Utf-8?B?QmlsbHkgQg==?=
<BillyB@discussions.microsoft.com> wrote:

>Larry,


>Sorry about any confusion with the way the question was written. You are
>close but I need to get any 25 words from the 125 available (25 words*5
>ws) not just 5 from each w.


>I am not sure what you mean about a secondary array or how I would do that.
>The randomizing does only happen when the program is started..


What Larry means is :-

Load all 125 words into an Array of Strings 1 to 125

Create an Array of Integers 1 to 125

Shuffle the Array of Integers

Take the first (say) 5 Integers and get the corresponding strings
eg: S$( I(1) ) S$( I(2) ) S$( I(3) ) ...

Private Sub Command1_Click()
Dim N(1 To 10) As Integer
Dim L9 As Long
Dim I As Long
Dim Hold As Long

Randomize ' just for example - should not be here

For L9 = 1 To UBound(N)
N(L9) = L9
Next

For L9 = UBound(N) To 1 Step -1
I = Int(L9 * Rnd) + 1
Hold = N(L9)
N(L9) = N(I)
N(I) = Hold
Next

Me.Cls
For L9 = 1 To UBound(N)
Me.Print N(L9);
Next

End Sub
Billy B

2006-04-29, 6:56 pm

Mr. French. Sorry this has become complicated. Maybe I can uncomplicate my
need. There are a total of ten ws in school quarter, more if a semester. I
give a mid-term and final spelling test. The need is, if the user clicks the
mid-term button, I need to get 25 random selection of words from however many
words there are from W 1 thru W 5 (just 1, 2,3,4,5 are stored in the
table). For the final, I need to do the same thing for W 6 thru 10 (same
numbering scheme.) Note: the table of words can have say 20 words in w 1,
25 in w 2, 15 in w three so I need to base the returned words on the
w field.

Hope that makes sense....

"J French" wrote:

> On Fri, 28 Apr 2006 19:50:01 -0700, =?Utf-8?B?QmlsbHkgQg==?=
> <BillyB@discussions.microsoft.com> wrote:
>
>
>
>
> What Larry means is :-
>
> Load all 125 words into an Array of Strings 1 to 125
>
> Create an Array of Integers 1 to 125
>
> Shuffle the Array of Integers
>
> Take the first (say) 5 Integers and get the corresponding strings
> eg: S$( I(1) ) S$( I(2) ) S$( I(3) ) ...
>
> Private Sub Command1_Click()
> Dim N(1 To 10) As Integer
> Dim L9 As Long
> Dim I As Long
> Dim Hold As Long
>
> Randomize ' just for example - should not be here
>
> For L9 = 1 To UBound(N)
> N(L9) = L9
> Next
>
> For L9 = UBound(N) To 1 Step -1
> I = Int(L9 * Rnd) + 1
> Hold = N(L9)
> N(L9) = N(I)
> N(I) = Hold
> Next
>
> Me.Cls
> For L9 = 1 To UBound(N)
> Me.Print N(L9);
> Next
>
> End Sub
>

Larry Serflaten

2006-04-29, 6:56 pm


"Billy B" <BillyB@discussions.microsoft.com> wrote
> Mr. French. Sorry this has become complicated. Maybe I can uncomplicate my
> need. There are a total of ten ws in school quarter, more if a semester. I
> give a mid-term and final spelling test. The need is, if the user clicks the
> mid-term button, I need to get 25 random selection of words from however many
> words there are from W 1 thru W 5 (just 1, 2,3,4,5 are stored in the
> table). For the final, I need to do the same thing for W 6 thru 10 (same
> numbering scheme.) Note: the table of words can have say 20 words in w 1,
> 25 in w 2, 15 in w three so I need to base the returned words on the
> w field.
>
> Hope that makes sense....


But returning a list of words based on the contents of one of the fields is better
accomplished using a properly formed SQL statement. What is the current SQL
you are using:

> Private Sub GetWords()
> Set rsTest = New clsRecSet
> rsTest.rsSpell.Open strSQL


What is the contents of strSQL?

LFS


Billy B

2006-04-29, 9:56 pm

Larry,

Here is the SQL statement:

strSQL = "SELECT W, Word, switch From Words WHERE (((W)=1 Or (W)=2
Or (W)=3 Or (W)=4 Or (W)=5));"

"Larry Serflaten" wrote:

>
> "Billy B" <BillyB@discussions.microsoft.com> wrote
>
> But returning a list of words based on the contents of one of the fields is better
> accomplished using a properly formed SQL statement. What is the current SQL
> you are using:
>
>
> What is the contents of strSQL?
>
> LFS
>
>
>

Larry Serflaten

2006-04-30, 3:56 am


"Billy B" <BillyB@discussions.microsoft.com> wrote
>
> Here is the SQL statement:
>
> strSQL = "SELECT W, Word, switch From Words WHERE (((W)=1 Or (W)=2
> Or (W)=3 Or (W)=4 Or (W)=5));"


So then it is apparent that you already have selected words from the desired ws:

"I want to randomly generate 25 words from the first five ws using the w
numbers 1, 2, 3, 4 and 5."

The SQL statement will ensure you get words whose w values match those
in the WHERE clause. (Although, (W>=1 And W<=5) seems easier to read ;-)

And you already know how to fill an array with the words:

txtCorrAns(intNum) = rsTest.rsSpell.Fields(1)

So it would seem what you would want to do is;

- Open the recordset and MoveLast to get a count of all the records returned.
- Create an array to hold the words
- MoveFirst and Loop through the recordset to fill the array
- Then shuffle the array.

But with all the confusion that has transpired, I have to wonder if that
is really what you want?

Here is a simple example of filling an array and then shuffling it. While not
the most efficient method, it would suffice. Note Randomize was left out so
it produces the same results every time. As was mentioned, Randomize
should be placed somewhere at the very start of your application.


Private Sub Form_Load()
Dim words() As String
Dim i As Long, swap As Long
Dim word As String, count As Long

count = 12 ' Number of months

ReDim words(1 To count)
Debug.Print vbCrLf & "ORIGINAL ORDER"
' Fill array
For i = 1 To count
words(i) = Format$(DateSerial(2006, i, 1), "mmmm")
Debug.Print words(i)
Next

' Shuffle
For i = 1 To count
swap = Int(Rnd * 12) + 1
If i <> swap Then
word = words(i)
words(i) = words(swap)
words(swap) = word
End If
Next

Debug.Print vbCrLf & "SHUFFLED"
For i = 1 To count
Debug.Print words(i)
Next

End Sub



LFS



Billy B

2006-04-30, 7:57 am

Larry,

Thank you very much.

"Billy B" wrote:
[color=darkred]
> Larry,
>
> Here is the SQL statement:
>
> strSQL = "SELECT W, Word, switch From Words WHERE (((W)=1 Or (W)=2
> Or (W)=3 Or (W)=4 Or (W)=5));"
>
> "Larry Serflaten" wrote:
>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com