473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 12040
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
6
by: VAL | last post by:
How can I make an application form that generates a unique number starting from 1001.?
4
by: Baz | last post by:
I want to randomly select some files from a source directory & copy them to a target folder for testing. How can I select files from the source directory randomly? I have thought about getting a...
4
by: Maziar Aflatoun | last post by:
Hi everyone, I have the following code in my class method TheSeed = (int)DateTime.Now.Ticks; Random rndNum = new Random(TheSeed); RandNum = rndNum.Next(0, TotalRows);...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.