469,621 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

Random Number Generating

I need a program that generate 5 non-duplicates random number between 1-10
as string values store in an array.

Do anybody know of any good books or websites that explain how to generator
random numbers using asp.net? I know about the random namespace within .net,
but
I need a reference to some code that do the similar stated function above.

Plus If you have any coding practice ideas for the above defined project
please share them.

Thanks!
Nov 16 '05 #1
16 11723
Leon,

I would use the Random class to do this. It seeds itself off the time
normally.

If you need a cryptographically secure random number generator, then
check out the RNGCryptoServiceProvider class in the
System.Security.Cryptography namespace.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leon" <vn*****@msn.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I need a program that generate 5 non-duplicates random number between 1-10
as string values store in an array.

Do anybody know of any good books or websites that explain how to
generator
random numbers using asp.net? I know about the random namespace within
.net, but
I need a reference to some code that do the similar stated function above.

Plus If you have any coding practice ideas for the above defined project
please share them.

Thanks!

Nov 16 '05 #2
Leon <vn*****@msn.com> wrote:
I need a program that generate 5 non-duplicates random number between 1-10
as string values store in an array.

Do anybody know of any good books or websites that explain how to generator
random numbers using asp.net? I know about the random namespace within .net,
but I need a reference to some code that do the similar stated function above.


If you're really just after 5 out of 10, I'd suggest having an array of
10 booleans to say whether or not the number has always been picked, an
array of results, and just keep picking a new random number until
you've found one which hasn't been picked before, mark it as picked,
and add its string representation to the output array. Do that until
you've got 5 numbers, and you're done.

If that's not enough to go on, please give details of which bit you're
having trouble with.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi Jon,

I kind of understand what you are saying jon, but could please give me an
example?

Array of 10 booleans? you mean the random class would only search the number
that are false, if true it has been picked right?
Add its string representation to the output array? so I should have two
arrays one search and one store the picked values as strings
Do that until you've got 5 numbers? Loop until I have five non-duplicate
ramdon numbers?

Thanks!
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Leon <vn*****@msn.com> wrote:
I need a program that generate 5 non-duplicates random number between
1-10
as string values store in an array.

Do anybody know of any good books or websites that explain how to
generator
random numbers using asp.net? I know about the random namespace within
.net,
but I need a reference to some code that do the similar stated function
above.


If you're really just after 5 out of 10, I'd suggest having an array of
10 booleans to say whether or not the number has always been picked, an
array of results, and just keep picking a new random number until
you've found one which hasn't been picked before, mark it as picked,
and add its string representation to the output array. Do that until
you've got 5 numbers, and you're done.

If that's not enough to go on, please give details of which bit you're
having trouble with.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Hi Leon,
I need a program that generate 5 non-duplicates random number between
1-10 as string values store in an array.


For this you do *not* need a random number generator, because your number
*must not be* random!

Random-Number generators might produce duplicate numbers!

You can do something like this:

<code>
bool [] numbers = new bool[10];

Random r = new Random();
int hits = 0;
while(hits < 5)
{
int pos = r.Next(9);
if (numbers[pos] == true)
continue;
numbers[pos] = true;
hits++;
}

Console.WriteLine("Random-Numbers are: ");
for(int i=0; i<10; i++)
{
if (numbers[i] == true)
Console.Write("{0} ", i.ToString());
}
Console.WriteLine();
</code>
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #5
Leon <vn*****@msn.com> wrote:
I kind of understand what you are saying jon, but could please give me an
example?

Array of 10 booleans? you mean the random class would only search the number
that are false, if true it has been picked right?
Yes.
Add its string representation to the output array? so I should have two
arrays one search and one store the picked values as strings
Do that until you've got 5 numbers? Loop until I have five non-duplicate
ramdon numbers?


Basically, yes.

using System;

class Test
{
static void Main()
{
Random rng = new Random();

bool[] found = new bool[10];
string[] results = new string[5];

for (int i=0; i < results.Length; i++)
{
while (true)
{
int index = rng.Next(10);
if (!found[index])
{
found[index]=true;
results[i]=index.ToString();
break;
}
}
}

foreach (string x in results)
{
Console.WriteLine (x);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jochen Kalmbach <no********************@holzma.de> wrote:
I need a program that generate 5 non-duplicates random number between
1-10 as string values store in an array.
For this you do *not* need a random number generator, because your number
*must not be* random!


On the contrary - he effectively first needs a random number between 0
and 9, then between 0 and 8, then between 0 and 7 etc.
Random-Number generators might produce duplicate numbers!


So you have to apply it slightly differently. You still need a random
number generator though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Hi Jochen,
Thanks for your response, but are you sure I don't need a random number
generator, because your code looks predictable.
the reason I need to use the random class is because I would like to feed
the program a seed ("such as time & ip address"), and the numbers may be
between 1 & 50, but the same solutions will apply.

"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn*********************************@207.46.24 8.16...
Hi Leon,
I need a program that generate 5 non-duplicates random number between
1-10 as string values store in an array.


For this you do *not* need a random number generator, because your number
*must not be* random!

Random-Number generators might produce duplicate numbers!

You can do something like this:

<code>
bool [] numbers = new bool[10];

Random r = new Random();
int hits = 0;
while(hits < 5)
{
int pos = r.Next(9);
if (numbers[pos] == true)
continue;
numbers[pos] = true;
hits++;
}

Console.WriteLine("Random-Numbers are: ");
for(int i=0; i<10; i++)
{
if (numbers[i] == true)
Console.Write("{0} ", i.ToString());
}
Console.WriteLine();
</code>
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 16 '05 #8
Hi Jon,
> I need a program that generate 5 non-duplicates random number
> between 1-10 as string values store in an array.


For this you do *not* need a random number generator, because your
number *must not be* random!


On the contrary - he effectively first needs a random number between 0
and 9, then between 0 and 8, then between 0 and 7 etc.


No!
In your example, you presume that the first "random" number is 9, the
second is 8, ...!!!
To be correct you need to do the following:

R = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} // set of numbers to choose from

a1 = random(R);

R1 = R without a1;
a2 = random(R1)

R2 = R1 without a2;
a3 = random(R2)

R3 = R2 without a3;
a4 = random(R3)

R4 = R3 without a4;
a5 = random(R4)
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:

<snip>

Note that that code produces 0-9, 1-10 - but it's a trivial tweak to
change the "base".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Leon <vn*****@msn.com> wrote:
Thanks for your response, but are you sure I don't need a random number
generator, because your code looks predictable.
the reason I need to use the random class is because I would like to feed
the program a seed ("such as time & ip address"), and the numbers may be
between 1 & 50, but the same solutions will apply.


The same solutions *don't* apply as the numbers get larger. That's why
I specified "If you're really after 5 out of 10" in my first post. The
same solutions are still feasible, but get more and more expensive.

For larger numbers, there are different approaches which could be used,
depending on whether the number of potential choices was much larger
than the number of results you want or whether they're close, etc. (For
instance, if you want nearly all the numbers, but in a random order, it
would be better to just pick the first n out of a random permutation.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
Hi Leon,
Thanks for your response, but are you sure I don't need a random
number generator, because your code looks predictable.


Of course... you need a random number generator... but you have to handle
it a little bit different...

And my example is from the mathematical point of view, really bad!

See my other reply to Jon Skeet. Here I showed what really shoud be done...
(but at the moment I do not know who to implement it...)
The example from Jon is also very bad (as bad as mine).

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #12
Jochen Kalmbach <no********************@holzma.de> wrote:
> I need a program that generate 5 non-duplicates random number
> between 1-10 as string values store in an array.

For this you do *not* need a random number generator, because your
number *must not be* random!
On the contrary - he effectively first needs a random number between 0
and 9, then between 0 and 8, then between 0 and 7 etc.


No!
In your example, you presume that the first "random" number is 9, the
second is 8, ...!!!


No I don't.

You seem to have assumed that I was just going to output the result of
the random number, which was never my intention at all.
To be correct you need to do the following:

R = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} // set of numbers to choose from

a1 = random(R);

R1 = R without a1;
a2 = random(R1)

R2 = R1 without a2;
a3 = random(R2)

R3 = R2 without a3;
a4 = random(R3)

R4 = R3 without a4;
a5 = random(R4)


Indeed - so on the first time, you need to pick one of ten numbers,
then one of nine, etc. That was what I was saying before.

You definitely *do* still need a random number generator (contrary to
your post), but you obviously can't just output the immediate result of
the call to the RNG.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
Hi Jon Skeet [C# MVP],
> On the contrary - he effectively first needs a random number between 0
> and 9, then between 0 and 8, then between 0 and 7 etc.
No!
In your example, you presume that the first "random" number is 9, the
second is 8, ...!!!


No I don't.


Maybe you just expressed it a little bit wrong:
<quote>
1. number between 0 and 9
2. number between 0 and 8
3. number between 0 and 7
and so on...
</quote>

But I am sure you meant:
1. select random number from 10 numbers
2. select random number from the remaining numbers
3. select random number from the remaining numbers
and so on...

You definitely *do* still need a random number generator


Yes! It was a bit misleading...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #14
Jochen Kalmbach <no********************@holzma.de> wrote:
Thanks for your response, but are you sure I don't need a random
number generator, because your code looks predictable.
Of course... you need a random number generator... but you have to handle
it a little bit different...


Indeed, and I don't think anyone has disputed that. It was your claim
that you *didn't* need a random number generator that struck me as
highly odd.
And my example is from the mathematical point of view, really bad!

See my other reply to Jon Skeet. Here I showed what really shoud be done...
(but at the moment I do not know who to implement it...)
Here's one potential way:

using System;

class Test
{
static Random rng = new Random();

static void Main()
{
Show (Permute (5, 10));
Show (Permute (5, 10));
Show (Permute (5, 10));
}

static void Show (int[] ints)
{
foreach (int i in ints)
{
Console.Write(i);
Console.Write(" ");
}
Console.WriteLine();
}

static int[] Permute (int length, int range)
{
int[] all = new int[range];
for (int i=0; i < all.Length; i++)
{
all[i] = i;
}

for (int i=0; i < length; i++)
{
int index = i+rng.Next(range-i);
if (index != i)
{
int tmp = all[i];
all[i] = all[index];
all[index] = tmp;
}
}

int[] result = new int[length];
Array.Copy (all, result, length);
return result;
}
}

It still takes as much space as the entire range, which may not be good
in some situations, but it's efficient in time. Basically it divides
the whole array, which initially contains all the values in order, into
the "chosen" section and "the rest", by swapping the non-chosen value
at any index with one of what's currently in "the rest" (allowing for
the possibility that the chosen value is the one at that index
already).

It would be nice to come up with a version which didn't have the space
constraint, but I can't easily think of anything which has neither
space nor time constraints.
The example from Jon is also very bad (as bad as mine).


It's not bad for what was originally required - getting 5 numbers out
of 10. It's not as efficient as it might be, but for the task at hand
that shouldn't make a difference - and it's simpler than the above.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15
Jochen Kalmbach <no********************@holzma.de> wrote:
Hi Jon Skeet [C# MVP],
> On the contrary - he effectively first needs a random number between 0
> and 9, then between 0 and 8, then between 0 and 7 etc.

No!
In your example, you presume that the first "random" number is 9, the
second is 8, ...!!!


No I don't.


Maybe you just expressed it a little bit wrong:
<quote>
1. number between 0 and 9
2. number between 0 and 8
3. number between 0 and 7
and so on...
</quote>

But I am sure you meant:
1. select random number from 10 numbers
2. select random number from the remaining numbers
3. select random number from the remaining numbers
and so on...


No, I meant a random number between 0 and 9. I didn't specify what was
then done with the number. You'll find that in another post, I do
exactly that - pick a number between 0 and 9, and then between 0 and 8.
I then use that number to find a number from the remaining ones.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #16
Hi Jon,
<quote>
1. number between 0 and 9
2. number between 0 and 8
3. number between 0 and 7
and so on...
</quote>
No, I meant a random number between 0 and 9. I didn't specify what was
then done with the number.


Ok, then I took the wrong assumption... sorry!
You'll find that in another post, I do
exactly that - pick a number between 0 and 9, and then between 0 and
8. I then use that number to find a number from the remaining ones.


Ok, then I just missinterpreted it... sorry!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by eric.gagnon | last post: by
6 posts views Thread by VAL | last post: by
4 posts views Thread by Baz | last post: by
4 posts views Thread by Maziar Aflatoun | last post: by
23 posts views Thread by Alvin | last post: by
40 posts views Thread by RadiationX | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.