|
-
Nov 3rd, 2002, 08:03 PM
#1
Thread Starter
Hyperactive Member
1-15 randomize?? anyone?
Hi all,
I have an array with 15 numbers (1 to 15)
1 2 3 5 6 7 8 9 10 11 12 13 14 15
how can I jumble them up randomly?? something like..
3 2 4 12 1 15 8 10 7 4 13 11 9 5 6
anyone?
thanks
Abe
1+1=3
make life simple, use a calculator!
-
Nov 3rd, 2002, 08:08 PM
#2
Hyperactive Member
one way (but not the best) could be to use 2 arrays, array 1 has values... then you generate 1-15, and that value in the new array = a random 1-15 in the "real array' and then once you do that, remove it from the normal array.
From there, you just do basic checking to see if there is a value there, so you don't get duplicates... Just reply if you want an example...
-
Nov 3rd, 2002, 08:13 PM
#3
Thread Starter
Hyperactive Member
re:
thanks..
how do I generate a random 1 to 15 number array?
1+1=3
make life simple, use a calculator!
-
Nov 3rd, 2002, 08:13 PM
#4
VB Code:
'Each click puts one number in the box
Private Sub Command1_Click()
numbers
End Sub
Private Sub numbers()
randomize
num = Int((15 * Rnd) + 1)
Text1.Text = Text1.Text & num
End Sub
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 3rd, 2002, 08:15 PM
#5
Hyperactive Member
for an array of 15 indexes with random numbers:
VB Code:
Randomize
Dim i As Integer
Dim arr(1 To 15) As Integer
For i = 1 To 15
arr(i) = Int(Rnd * 15) + 1
Print arr(i)
Next i
just note that you may have repeated numbers, so you'll have to add code for unique numbers.
-
Nov 3rd, 2002, 08:18 PM
#6
Addicted Member
there is one small problem if ur gonna use random numbers that is that what if one number appears more than once?
I think the best way to do this then would be make a random number between 1 and 20 and if its 1 then the numbers are 4 3 6 12 5... (ones that you programmed in)
First person to be able to get what song is currently playing in Winamp 3.
-
Nov 3rd, 2002, 08:23 PM
#7
Thread Starter
Hyperactive Member
thanks,
i guess then there is no fast way of doing it... and hece the computer will ahve to loop many times until it gets and picks random numbers which it is yet to choose to make the the complete 15 uneque random numbers.
coz, what I need is that, to have 15 numbers, 1 to 15, but in jumbled order.
do you know what i mean guys?
1+1=3
make life simple, use a calculator!
-
Nov 3rd, 2002, 08:44 PM
#8
Try something like this
VB Code:
Dim intLoop As Integer
Dim intInner As Integer
Dim arrValues(15) As Integer
Dim arrBOolean(15) As Integer
Dim x As Integer
Dim blnVal As Boolean
For intLoop = 0 To 14
arrBOolean(intLoop) = False
Next
For intLoop = 0 To 14
blnVal = True
While blnVal
x = Rnd(203) * 14 + 1
If Not arrBOolean(x) Then
arrValues(intLoop) = x
arrBOolean(x) = True
blnVal = False
End If
End While
Next
TextBox1.Text = ""
For intLoop = 0 To 14
TextBox1.Text = TextBox1.Text & "Arrvalue(" & intLoop & ") = " & arrValues(intLoop) & vbCrLf
Next
Hope this helps,
-
Nov 3rd, 2002, 08:58 PM
#9
^:^...ANGEL...^:^
I use this code if that solves ur problem...
VB Code:
Option Explicit
Private Sub Form_Load()
Dim i As Integer 'Loop Counter
Dim usedNumbers(50) As Integer 'hold the used numbers
Dim CurrentNumber As Integer 'The current random number
Dim j As Integer 'Loop Counter
Dim k As Integer 'Holds our place in the UsedNumbers Array
Dim BadNumber As Boolean 'Is it a bad number?
For i = 0 To 49
CurrentNumber = Int(Rnd() * 50) + 1
'Ensure that you get numbers between 1 and 50 Int(Rnd() * 50) + 1
For j = 0 To 49
'Check the new number against every other number we've generated
If (CurrentNumber = usedNumbers(j)) Then
BadNumber = True
Exit For
End If
If (usedNumbers(j) = 0) Then
'We will only have zeroes in the array if no value has been entered
'in used Numbers for that index so we can safely leave if we see a zero
Exit For
End If
Next j
While BadNumber
'if its a bad number we need to keep generating randome numbers until we have good one
BadNumber = False
CurrentNumber = Int(Rnd() * 50) + 1
For j = 0 To 49
'Do the same check to see if it is a bad number
If (CurrentNumber = usedNumbers(j)) Then
BadNumber = True
Exit For
End If
Next j
Wend
'You won't get here unless you have a good number
usedNumbers(k) = CurrentNumber
k = k + 1
Debug.Print CurrentNumber
Next i
End Sub
Cheers...
-
Nov 3rd, 2002, 11:40 PM
#10
Just swap the numbers, like that numbers don't repeat...
VB Code:
Public Sub RandomizeNumbers(Arr() As Integer)
Dim K As Integer
Randomize
' how many iterations the loop makes does not matter
' but it should be more than UBound(Arr) to make sure all
' numbers are swapped
For K = 0 To UBound(Arr) * 2
Swap Arr(Fix((UBound(Arr) + 1) * Rnd)), Arr(Fix((UBound(Arr) + 1) * Rnd))
Next K
End Sub
Public Sub Swap(ByRef Num1 As Integer, ByRef Num2 As Integer)
Dim Tmp As Integer
Tmp = Num1
Num1 = Num2
Num2 = Tmp
End Sub
-
Nov 3rd, 2002, 11:43 PM
#11
Actually... this is better...
VB Code:
Public Sub RandomizeNumbers(Arr() As Integer)
Dim K As Integer
Randomize
For K = 0 To UBound(Arr)
Swap Arr(K), Arr(Fix((UBound(Arr) + 1) * Rnd))
Next K
End Sub
Public Sub Swap(ByRef Num1 As Integer, ByRef Num2 As Integer)
Dim Tmp As Integer
Tmp = Num1
Num1 = Num2
Num2 = Tmp
End Sub
-
Nov 4th, 2002, 12:52 AM
#12
PowerPoster
Ok, this way will be the simplest solution to your problem... interesting solutions guys... 
VB Code:
'Create initial question list - max questions
ReDim MyArray(intNumberOfElements) As Integer
'Fill intQuestionList with initial data
Dim i As Integer
For i = 0 To (UBound(MyArray)-1)
MyArray(i) = i
Next
'Randomize question list
Dim temp As Integer
Dim j As Integer
For i = 1 To UBound(MyArray)
Randomize
j = (Rnd(1) * (UBound(MyArray) - 1)) + 1
temp = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = temp
Next
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Nov 4th, 2002, 07:48 AM
#13
Thread Starter
Hyperactive Member
re:
just like to say thank you for everyone's great input to this.
Some really very interesting ways round it too!
thanks guys
Abe
1+1=3
make life simple, use a calculator!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|