Welcome to Dream.In.Code
Become a VB Expert!

Join 137,233 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,759 people online right now. Registration is fast and FREE... Join Now!




Best way to randomize images?

 
Reply to this topicStart new topic

Best way to randomize images?

cosmo
17 Feb, 2007 - 02:46 PM
Post #1

New D.I.C Head
*

Joined: 17 Feb, 2007
Posts: 1


My Contributions
hi all,

looking for a bit of guidance

trying to write a small program (trying being the word!) at the mo, and the main form resembles that of a calculator, buttons 0-9, OK button etc

however, what i want it to do is every time i start the program, i want to know if it's possible to jumble up the buttons (so the 1 is where the 5 is, and the 9 where the 2 is etc) completely at random, has no set pattern

what would be the best way (really meaning the easiest!!) of implementing this?

THanks in advance!
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Best Way To Randomize Images?
18 Feb, 2007 - 04:07 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
use a listbox to load ID of each box,
myRandom = int(rnd * list.itemcount)

list.index(myRandom).removeitem will give you a continually reducing list
as you remove an item from the list, place it on the grid


This post has been edited by KeyWiz: 18 Feb, 2007 - 04:15 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Best Way To Randomize Images?
18 Feb, 2007 - 05:29 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
I am a math geek so I like this example. In a form I added 9 buttons arranged like they might be on a calculator or phone and add the following code to the form:

CODE

Option Explicit


Private Sub Form_Load()
    Dim i As Integer
    Dim v As Integer
    For i = 0 To 8
        Command1(i).Caption = ""
    Next i
    AssignCaptions
End Sub

Private Sub Command1_Click(Index As Integer)
    AssignCaptions
End Sub


'Randomly assign each button a number
'With no repetes...
Private Sub AssignCaptions()
    Dim i As Integer
    Dim v As Integer
    Randomize Timer
    v = Rnd * 8
    For i = 0 To 8
        v = (4 * v + 4) Mod 9
        Command1(i).Caption = v + 1
    Next i
End Sub


THe heart of this is something called a Linear Congruential Generator and they are the same things that are used is most psudo-random number generators out there.

The formula v=(4*v+4) mod 9 is a LCG and it will generate a nice sequence of scambled numbers.

Problem. In my rush to apply math I didn't think this though very well. Each time it generated the same sequence 1, 5, 3, 4, 8, 6, 7, 2, 9 but starting at different points (8, 6, 7, 2, 9, 1, 5, 3, 4 for example). The LCG generates a sequence of numbers and the initial value of v causes the shift, but if the other two values (v=(a*v+b ) mod m) in this case a and b do not change then the sequence is the same except the shift.

So here is another version. Yea, I know it is not the simple solution you were looking for but I am having too much fun to stop.

Here is the corrected sub:

CODE

'Randomly assign each button a number
'With no repetes...
Private Sub AssignCaptions()
    Dim i As Integer
    Dim v As Integer
    Dim a As Integer
    Dim b As Integer
    Randomize Timer
    b = Rnd * 8 + 1
    a = (Int(Rnd * 2) + 1) * 3 + 1
    If b = 1 Or b Mod 3 = 0 Then b = b + 1
    v = Rnd * 9
    For i = 0 To 8
        v = (a * v + b) Mod 9
        Command1(i).Caption = v + 1
    Next i
End Sub


I know this is not helping. Sorry.

This post has been edited by NickDMax: 18 Feb, 2007 - 05:31 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:44PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month