Random number again

P

Preschool Mike

I received help with the following code a few days ago and it works great for
what I wanted (e.g. come up with 12 different random numbers). Now I'm
trying to adjust it for something different (shuffle 24 different random
numbers) but I don't understand how the code works. I've tried plugging
numbers in here and there but not getting good results. I would appreciate
an explanation of how it works and help with correcting it for 24 numbers so
if future adjustments need to be made I can do them.

Thanks so much
 
P

Preschool Mike

forgot to put in code on last post.

Sub SetRandomNumbers()
Dim raynum(1 To 12)
Dim i As Integer
Dim Num_a As Integer
Dim Num_b As Integer
Dim Num_c As Integer
Dim Num_d As Integer
Dim stRresult As String
'fill the array
For i = 1 To 12
raynum(i) = i
Next i
'shuffle
For i = 1 To 20
Randomize
Num_a = Int((12 * Rnd) + 1)
Num_b = Int((12 * Rnd) + 1)
Num_c = raynum(Num_a)
Num_d = raynum(Num_b)
raynum(Num_b) = Num_c
raynum(Num_a) = Num_d
Next i
For i = 1 To 12
stRresult = stRresult & raynum(i) & "/ "
Next i

MsgBox stRresult
End Sub
--
Mike Mast
Special Education Preschool Teacher


Preschool Mike said:
I received help with the following code a few days ago and it works great for
what I wanted (e.g. come up with 12 different random numbers). Now I'm
trying to adjust it for something different (shuffle 24 different random
numbers) but I don't understand how the code works. I've tried plugging
numbers in here and there but not getting good results. I would appreciate
an explanation of how it works and help with correcting it for 24 numbers so
if future adjustments need to be made I can do them.

oops forgot the code.
 
M

Mark

forgot to put in code on last post.

Sub SetRandomNumbers()
Dim raynum(1 To 12)
Dim i As Integer
Dim Num_a As Integer
Dim Num_b As Integer
Dim Num_c As Integer
Dim Num_d As Integer
Dim stRresult As String
'fill the array
For i = 1 To 12
raynum(i) = i
Next i
'shuffle
For i = 1 To 20
Randomize
Num_a = Int((12 * Rnd) + 1)
Num_b = Int((12 * Rnd) + 1)
Num_c = raynum(Num_a)
Num_d = raynum(Num_b)
raynum(Num_b) = Num_c
raynum(Num_a) = Num_d
Next i
For i = 1 To 12
stRresult = stRresult & raynum(i) & "/ "
Next i

MsgBox stRresult
End Sub
--
Mike Mast
Special Education Preschool Teacher



oops forgot the code.






- Show quoted text -
You should be able to just change the 12 to a 24 everywhere it appears
(or better yet, set it to a variable and use that everywhere). I
havent' looked at the code you are using too much, so I'm not quite
sure why the one loop goes from 1 to 20.

The code I posted earlier had the code to shuffle the array in a
separate function, so you wouldn't have to rewrite it to handle a
different sized array. If you use that, then you don't have to
duplicate all of that code if you want to work with multiple different
sized lists of numbers in the same program.
 
D

David Marcovitz

Mike,

The "fill the array section" fills an array so that

raynum(1) = 1
raynum(2) = 2
....
raynum(12) = 12

This is not very random, so the "shuffle" section of the code randomize
it. It basically randomly picks two items in the array and swaps them.
Let's say it picks 5 and 8. After one pass you would then have

raynum(1) = 1
raynum(2) = 2
....
raynum(5) = 8
....
raynum(8) = 5
....
raynum(12) = 12

That is, all numbers are the same except the two it swapped.

Because this section is a loop, it does 20 random swaps. With 12
numbers, doing 20 swaps should do a pretty good job shuffling, even if
occasionally, the same slots got swapped.

So, the first thing you need to do is change each 12 to 24:

Dim raynum(1 To 12) is just creating an array that can hold 12 different
numbers. If you make it 1 To 24, it will hold 24 numbers.

For i = 1 To 12 is used to loop through the array so you are looking at
each of the 12 items in the array with raynum(i). If raynum now has 24
items, you need For i = 1 To 24 to loop through all 24 items in the array.

Int((12 * Rnd) + 1) is the formula to give you a random number between 1
and 12. It is used in this code to randomly pick one of the 12 slots to
swap with another of the 12 slots. If you have 24 slots, you need a
number between 1 and 24; thus, you need Int((24 * Rnd) + 1)

Finally, the shuffling loop of For i = 1 To 20. This is used to perform
20 swaps, which will give a nice shuffling if there are only 12 slots,
but it won't be too good if there are 24. This number can really be as
large as you want, but if 20 works well for 12 slots, I think 40 will be
good for 24 slots so you should make it For i = 1 To 24

Does that make sense?

--David

Mike

In the code there are 5 occurrances of "12" - change them all to "24".
-- john ATSIGN PPTAlchemy.co.uk Free PPT Hints, Tips and Tutorials
http://www.pptalchemy.co.uk/powerpoint_hints_and_tips_tutorials.html


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
P

Preschool Mike

Thanks John that worked great. I thought I had tried that before I asked for
help but I must have left something out.
 
P

Preschool Mike

Thanks for you help Mark. The problem wasn't with your original code - It
was with me. I don't know a lot of vba (still in the learning process),
especially how to use Functions - Plain and simple I didn't know how to use
the code, still a lot to learn.
 
D

David Marcovitz

Mike,

The "fill the array section" fills an array so that

raynum(1) = 1
raynum(2) = 2
....
raynum(12) = 12

This is not very random, so the "shuffle" section of the code randomize
it. It basically randomly picks two items in the array and swaps them.
Let's say it picks 5 and 8. After one pass you would then have

raynum(1) = 1
raynum(2) = 2
....
raynum(5) = 8
....
raynum(8) = 5
....
raynum(12) = 12

That is, all numbers are the same except the two it swapped.

Because this section is a loop, it does 20 random swaps. With 12
numbers, doing 20 swaps should do a pretty good job shuffling, even if
occasionally, the same slots got swapped.

So, the first thing you need to do is change each 12 to 24:

Dim raynum(1 To 12) is just creating an array that can hold 12 different
numbers. If you make it 1 To 24, it will hold 24 numbers.

For i = 1 To 12 is used to loop through the array so you are looking at
each of the 12 items in the array with raynum(i). If raynum now has 24
items, you need For i = 1 To 24 to loop through all 24 items in the array.

Int((12 * Rnd) + 1) is the formula to give you a random number between 1
and 12. It is used in this code to randomly pick one of the 12 slots to
swap with another of the 12 slots. If you have 24 slots, you need a
number between 1 and 24; thus, you need Int((24 * Rnd) + 1)

Finally, the shuffling loop of For i = 1 To 20. This is used to perform
20 swaps, which will give a nice shuffling if there are only 12 slots,
but it won't be too good if there are 24. This number can really be as
large as you want, but if 20 works well for 12 slots, I think 40 will be
good for 24 slots so you should make it For i = 1 To 24

Does that make sense?

--David

Mike

In the code there are 5 occurrances of "12" - change them all to "24".
-- john ATSIGN PPTAlchemy.co.uk Free PPT Hints, Tips and Tutorials
http://www.pptalchemy.co.uk/powerpoint_hints_and_tips_tutorials.html


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top