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
Printable View
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
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...
thanks..
how do I generate a random 1 to 15 number array?
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
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.
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)
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?
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,
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...
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
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
Ok, this way will be the simplest solution to your problem... interesting solutions guys... :p
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
just like to say thank you for everyone's great input to this.
Some really very interesting ways round it too!
thanks guys
Abe