Click here to Skip to main content
15,913,773 members
Home / Discussions / C#
   

C#

 
General4 different random numbers Pin
insurgentpyro6-Jul-04 11:20
insurgentpyro6-Jul-04 11:20 
GeneralRe: 4 different random numbers Pin
Christian Graus6-Jul-04 11:57
protectorChristian Graus6-Jul-04 11:57 
GeneralRe: 4 different random numbers Pin
insurgentpyro6-Jul-04 18:50
insurgentpyro6-Jul-04 18:50 
GeneralRe: 4 different random numbers Pin
Christian Graus7-Jul-04 12:22
protectorChristian Graus7-Jul-04 12:22 
GeneralRe: 4 different random numbers Pin
Anonymous7-Jul-04 17:51
Anonymous7-Jul-04 17:51 
GeneralRe: 4 different random numbers Pin
Arjan Einbu6-Jul-04 12:29
Arjan Einbu6-Jul-04 12:29 
GeneralRe: 4 different random numbers Pin
insurgentpyro6-Jul-04 18:52
insurgentpyro6-Jul-04 18:52 
GeneralRe: 4 different random numbers Pin
Arjan Einbu7-Jul-04 1:34
Arjan Einbu7-Jul-04 1:34 
 1: using System.Collections;
 2: public class RandomCollection
 3: {
 4:	private Random rnd = new Random();
 5:	public int[] RandomSample(int count, int low, int high)
 6:	{
 7:		ArrayList rnds = new ArrayList();
 8:		for(int i = 0; i < count; i++)
 9:		{
10:			int t = rnd.Next(low, high - i);
11:			foreach(int r in rnds)
12:			{
13:				if(t >= r) t++;
14:			}
15:			rnds.Add(t);
16:		}
17:		rnds.Sort(); //NOT optional...
18:		return (int[])rnds.ToArray(typeof(int));
19:	}
20: }

The card deck is represented as the numbers 1 through 52.

- First you pick a card from the 52 cards available. In line 11, i is 0, so the statement executed does something like int t = rnd.Next(1, 52);. Line 13 isn't executed, since the rnds ArrayList is still is empty.
t represents the first card.

- Second you pick a card from the 51 cards left. Since i is 1, line 11 will dosomething like int t = rnd.Next(1, 51);.
If t is equal to or higher than the first card's number; Add 1. (So that t is in the range of 1 through 52, except for the first card's number.)

- Third you pick a card from the 50 cards left...
If t is equal to or higher than the first card's number; Add 1. (So that t is in the range of 1 through 51, except for the first cards number.)
If t is equal to or higher than the second card's number; Add another 1. (So that t is in the range of 1 through 52, except for the first to cards' number.)

- Fourth you pick a card from the 49 ones left in the deck...
If t is equal to or higher than the first card's number; Add 1. (So that t is in the range of 1 through 50, except for the first cards number.)
If t is equal to or higher than the second card's number; Add another 1. (So that t is in the range of 1 through 51, except for the first to cards' number.)
If t is equal to or higher than the second cards number; Add yet another 1. (So that t is in the range of 1 through 52, except for the first three cards' number.)

There you have it...

NB: The rnds.Sort(); statement in line 17 is NOT optional as previously stated...


Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
GeneralRe: 4 different random numbers Pin
Anonymous7-Jul-04 5:39
Anonymous7-Jul-04 5:39 
GeneralRe: 4 different random numbers Pin
Arjan Einbu7-Jul-04 10:27
Arjan Einbu7-Jul-04 10:27 
GeneralRe: 4 different random numbers Pin
mikeschuld6-Jul-04 13:18
mikeschuld6-Jul-04 13:18 
GeneralRe: 4 different random numbers Pin
insurgentpyro6-Jul-04 18:50
insurgentpyro6-Jul-04 18:50 
GeneralRe: 4 different random numbers Pin
mikeschuld7-Jul-04 21:52
mikeschuld7-Jul-04 21:52 
QuestionC++ Objects in C#? Pin
So and So6-Jul-04 8:16
So and So6-Jul-04 8:16 
AnswerRe: C++ Objects in C#? Pin
Dave Kreskowiak6-Jul-04 8:31
mveDave Kreskowiak6-Jul-04 8:31 
AnswerRe: C++ Objects in C#? Pin
Heath Stewart6-Jul-04 10:03
protectorHeath Stewart6-Jul-04 10:03 
GeneralRe: C++ Objects in C#? Pin
leppie6-Jul-04 10:48
leppie6-Jul-04 10:48 
GeneralRe: C++ Objects in C#? Pin
Heath Stewart6-Jul-04 10:53
protectorHeath Stewart6-Jul-04 10:53 
GeneralRe: C++ Objects in C#? Pin
MKlucher6-Jul-04 11:22
MKlucher6-Jul-04 11:22 
GeneralRe: C++ Objects in C#? Pin
Heath Stewart6-Jul-04 12:20
protectorHeath Stewart6-Jul-04 12:20 
GeneralRe: C++ Objects in C#? Pin
MKlucher6-Jul-04 13:13
MKlucher6-Jul-04 13:13 
GeneralRe: C++ Objects in C#? Pin
Stefan Troschuetz6-Jul-04 21:16
Stefan Troschuetz6-Jul-04 21:16 
GeneralRe: C++ Objects in C#? Pin
Heath Stewart7-Jul-04 2:54
protectorHeath Stewart7-Jul-04 2:54 
GeneralVolume Controls and interop Pin
Oldmate6-Jul-04 4:38
Oldmate6-Jul-04 4:38 
GeneralRe: Volume Controls and interop Pin
Heath Stewart6-Jul-04 5:10
protectorHeath Stewart6-Jul-04 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.