## wyett

Alright, I'm trying to generate an ODD random number between 1 and 8.

``````Do While (i < 4)
box(i) = randomObject.Next(1, 8)
Do While (flag = 0)
If (box(i) Mod 2 = 0) Then
k = 0
box(i) = randomObject.Next(1, 8)
If (box(i) = box(k + 1)) Then
box(i) = randomObject.Next(1, 8)
ElseIf (box(i) = box(k + 2)) Then
box(i) = randomObject.Next(1, 8)
ElseIf (box(i) = box(k + 3)) Then
box(i) = randomObject.Next(1, 8)
Else
box(i) = box(i)
End If

Else
k = 0
If (box(i) = box(k + 1)) Then
box(i) = randomObject.Next(1, 8)
ElseIf (box(i) = box(k + 2)) Then
box(i) = randomObject.Next(1, 8)
ElseIf (box(i) = box(k + 3)) Then
box(i) = randomObject.Next(1, 8)
Else
Exit Do
End If
End If

flag = 1
Loop
i = i + 1
Loop``````

What I'm having trouble with is that the numbers MUST be unique. So, only one 1, one 3, one 5, one 7. So I have it go through the second statements to randomize the object again if it's the same, but after it randomizes, I kind of need it to go back to the top to see if the number is even again. I've been working on this for hours and I'm a bit frustrated.

Can anyone point me in the right direction with this, please?

## apegram 302

Rethink the problem. At least the "odd" part.

Instead of generating a number from 1 to 8 and then checking if it's odd, instead consider generating a number from 0 to 3. Multiply that number by 2 and add 1. It will always be odd.

0 -> 1
1 -> 3
2 -> 5
3 -> 7

Now all you have to worry about is the whole "one time only" bit.

Another way to do it is to skip the whole random generation thing and instead apply a random sort. Assign values of 1,3,5, and 7 and then sort on something random. That way, you do not have to worry about exclusivity or odd versus even, as you have already ensured both.

## wyett

Ahh, that would make it much simpler if I just multiple the value by 2 and add 1.

``````Do While (i < 4)
box(i) = ((randomObject.Next(3) * 2) + 1)
Do While (flag = 0)
k = 0
If (box(i) = box(k + 1)) Then
box(i) = ((randomObject.Next(3) * 2) + 1)
ElseIf (box(i) = box(k + 2)) Then
box(i) = ((randomObject.Next(3) * 2) + 1)
ElseIf (box(i) = box(k + 3)) Then
box(i) = ((randomObject.Next(3) * 2) + 1)
Else
flag = 1
End If
Loop
i += 1
Loop``````

I still end up getting duplicates. I need to use this method, as my professor wants it done this way, but I'm still having issues of getting duplicates. :-\ Any ideas on that?

## wyett

I re-read the assignment and found I could do it the alternate way you suggested. Below is the solution I came to.

``````For x As Integer = 0 To 3
temp = randomObject.Next(3)
temp1 = box(temp)
box(temp) = box(x)
box(x) = temp1
Next x``````

Works beautifully! Thanks! (and that definitely shortened the lines of code)