please help me with get random value

quangha

Member
Joined
Jan 30, 2010
Messages
16
Programming Experience
Beginner
Please help me, i want to get a random number from an integer array, then delete that number in that array.
for example, i have an array like this {1,2,3,4,5,6,7,8,9}
now i want to get a RANDOM number in that array ( the number must be in the array), 8 for example. And then delete it from the array.
This is the array should be when done {1,2,3,4,5,6,7,9}
Thanks for reading
 
This is a Console app showing how it can be done:

VB.NET:
Module Module1
	'Delete random number from array
	Sub Main()
		Dim size As Integer, siz As String
		Console.Write("Enter size of array:  ")
		siz = Console.ReadLine()
		Integer.TryParse(siz, size)
		Dim myarray(size) As Integer
		Dim randnum As New Random
		Dim nonum, y As Integer
		nonum = randnum.Next(1, size + 1)
		Console.WriteLine(nonum)
		For x As Integer = 1 To size	'initialize array with sequential values
			myarray(x) = x
		Next x
		y = 0
		For x As Integer = 1 To size - 1	'you will have one less
			y += 1
			If myarray(x) = nonum Then y += 1
			myarray(x) = myarray(y)
			Console.Write(myarray(x) & ", ")
		Next
		ReDim Preserve myarray(size - 1)	'reduce size of array to eliminate the last element
		Console.WriteLine()
		Console.ReadLine()
	End Sub

End Module
 
thank you for fast reply. But I want to use the random number first, then delete it ( not deleting a random number from array, it is getting a random number, use it and delete it after using) I' m very very new to VB.NET, so I don't understand when take a look at your code ( especially console app ). Thank you
 
You are not explaining yourself clearly. I did EXACTLY what you asked for in your first post, which was to delete a random number from the array. You gave a before and after example, and that's exactly what my code does.

Now you are changing your mind about what you want the program to do, and you are STILL not clear!!

The Console app is very easy to run. Just select Console instead of Windows Form from the dialog box. Copy and paste the code directly into the edit window and run it. It shows the selected random number, and the output after the random number was deleted.
 
I'm really sorry for making you misunderstanding. actually, i want to make a multiple-choice exam making program. I want to organize the questions and solutions randomly. My idea is : I have an array {1,2,3,4}, chose 1 random number from that array, 4 for example, then I will place the 4TH question into the first place in the exam. then delete 4 from array and so on, so that the program won't made mistake when choosing 4 twice. Thanks for your concern, I hope you can continue to teach me about random value, thanks again
 
What you are really looking for is a shuffle sort, which will select a group of random numbers without omitting or repeating.

This sample uses a button and a textbox. The textbox will display the array values shuffled into a random order. Select the values one by one to use in your program.

VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim randnum As New Random()
		Dim mix, temp, num As Integer
		Dim randarray() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8}
		num = 8
		For x As Integer = 1 To num
			mix = randnum.Next(1, num + 1)
			temp = randarray(mix)
			randarray(mix) = randarray(x)
			randarray(x) = temp
		Next x
		TextBox1.Clear()
		For x As Integer = 1 To num
			TextBox1.Text &= randarray(x).ToString & "  "
		Next x
	End Sub
 
Do NOT send me any private messages. I am a teacher and I do not do homework for students. However, the shuffle sort is not generally part of an introductory course for beginners so I provided the code for you. If you try to submit it as a homework assignment, your teacher will know you didn't do it yourself.

I am not your personal assistant. If you need a programming question answered, then post it on the forum. Anyone who is available can read it and reply to it.
 
i'm sorry. I'm a student of foreign language and fond of programming, I'm not an IT student and this is not my homework, this is my own idea to help get me some exp on programming. I'm spending my time in the lunar newyear to learn VB.NET, don't misunderstand me, I never have someone did my homework for me, i just send you a message just want to say my thanks.
 
Check wiki for "Fisher Yates shuffle" to get an unbiased shuffle algo.
Apply this to your array (wiki contain vb.net sample code iirc) and then do NOT delete from the array, but instead simply loop over it.

a() = 1,2,3,4
shuffle a !
a() = 2,4,1,3
ask a(0)
ask a(1)
ask a(2)
...
 
Interesting reference, although the code you posted makes no sense in VB. I looked up the Knuth-Fisher-Yates shuffle and read the post about the "naive algorithm." I amended my code from:

VB.NET:
		For x As Integer = 1 To num
			mix = randnum.Next(1, num + 1)
                        temp = randarray(mix)
			randarray(mix) = randarray(x)
			randarray(x) = temp
		Next x
to:

VB.NET:
		For x As Integer = 1 To num
			mix = randnum.Next(x, num + 1)	'use x instead of 1 for first argument
			temp = randarray(mix)
			randarray(mix) = randarray(x)
			randarray(x) = temp
		Next x


This may be more efficient and more accurate.
 
If your Elements go from "m" to "n", the outer "for" loop should be from m to n-1 and not n (!) because the final random would otherwise go from n to n+1 which means that the result of random is always n, effectively meaning that element n ist always exchanged with itself - which of course isnt't "wrong", but useless.
 
Back
Top