Assigning Random Numbers

liunx

Guest
I want to assign a random integer from the range 1-13 to 13 people so that each one has a unique number from that range. Any help?

Thanks~Dont know what language you are using but in C# the following code fills the resultsList with 13 unique integer from 1-13. Not the most memory efficient method of doing it but im tired. The lists are used instead of arrays becaus you cant add or remove elements to or from an array after creation.


using System.Collections.Generic;
///
//then the normal stuff
///

int n = 0;
List<int> shuffleList = new List<int>(13);
List<int> resultsList = new List<int>(13);
while(n<13)
{
shuffleList.Add(n);
n++;
}
Random rand = new Random(System.DateTime.Now.Second);
while (shuffleList.Count > 0)
{
int a = rand.Next(0, shuffleList.Count - 1);
resultsList.Add(shuffleList[a]+1);
shuffleList.RemoveAt(a);
}
//Now resultsList contians 13 int elements each one unique and between 1-13


n.b. actually it might be quite memory efficient as the list elements, once removed are available for garbage collection i believe. Wheras an array would not make any memory available for garbage collection untill it has been disposed (or the program ends).I am working in ASP/VBScript. Any ideas on that?dimeric, that is incorrect when threading in ASP.net, it will result in the same number on the page no matter what you do.

You should use the RNGRandomNumberGenerator class from the System.Security.Cryptography namespace.

Bobba you can do your task in ASP using

Randomize() function and calling Rnd in each call to get a unique number.How?
Surely if it is seeded with the current seconds each time then the series will have one of 60 different seeds, each of which produces a differenet random number series (i release that these numbers are random wrt each other not random in the sense that it produces a different random number each time it is called.).

Although you are right that you would be better using the crypt randomn number class.Because web pages execute in thousands of a second. not seconds. They are proseduo-random numbers, meaning using the same seed results in the same number.Yeah but you are using the current number of seconds not the execution time!

Also random numbers are (mathmatically speaking) a series of numbers generated by an algorithm that you cannot spot a pattern in. It isn't a single number that appears to be randomly chosen.

My method produces 60 different series of random numbers. If you want to produce more random numbers you dont re-initialise the random number generator you continue to use the same series.ok a page that starts running at 12:00:00:001 and ends at 12:00:00:045, is still in the same second.yeah but it doesnt matter, you only seed it once? It then gives you a series of random numbers (random wrt each other)That sorta true. The issue is that the first in the seed is always the same. if you don;'t re-create the object then your correct, however in the reverse its not.
 
Back
Top