Random Number Generating

L

Leon

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?

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

Thanks!
 
S

Scott Mitchell [MVP]

Leon said:
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?

Leon, in .NET you can use the Random class to generate a random number.
See the technical docs at http://tinyurl.com/5b8m4

For your particular problem I'd do the following:

(1) Create an ArrayList with 10 elements, the numbers 1 through 10
(2) In a for loop that iterates 5 times, randomly choose a number, n,
between 1 and the size of the ArrayList.
(3) Your randomly chosen number is the number at element n in the
ArrayList. Remove this element from the ArrayList and go back to step (2).

This approach guarantees you 5 unique random numbers with 5 tries. hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
P

Patrice

See System.Math.Random in the class documentation (though I don't know if a
number is really "random" if you require it not to be the same than a
previous number).

You could have an array that tells if this partiuclar number is already
taken allowing to pick a new random number. You could use use a list if you
have a bigger interval (so that you can browse already picked numbers rather
than to have a boolean for each possible number).

Patrice
 
L

Leon

I want to thank all you guys for your responses! But out all the responses
which is the best way to solve my problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top