472,984 Members | 2,122 Online

# random number generator

I need some help writing this function

the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.

string randGen(int maxRange)

maxRange=1000
the output would be:

total of 1000 numbers from 1 - 1000 randomly
2,6,3,423,123,673,23,53...

I know how to write the random number generator part but im having trouble
with the non-duplicate part.
any help is appreciated.

Aaron
Jul 21 '05 #1
8 2386
Boy, that sounds like homework.

I'll help you, primarily to point you in the right direction. If this is a
professional app, let me know if I'm being to "cagey".

What you have: a random number generator that generates a single value in a
range.
What you need: to order all of the numbers in a range in a random sequence.

Your need is substantially different than what you have. So you cannot
approach the problem directly.

Look at the "what you need" statement. You have a requirement: all of the
numbers in a range. Start there.
How would you create a list of all of the numbers in a range?

Now, look at the other part of your requirement: arrange them in a random
sequence.

So, each of the numbers in your range has a "natural" position, but we need
to ignore that, and give each of your numbers a new position.

So each of your numbers needs to have another value attached to it: the new
position.
In other words, you have pairs. The number and it's new position.
How would you make a list of pairs?

How would you create a random number for the new position? Would that
position value have to be in the same range as the original list of numbers?

How would you reorder your list based on the new position value?

How would you print out the ordered list?

(As you can see, I broke down your problem into steps and hopefully gave
guidance without giving away the method.)

Good Luck,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Aaron" <ku*****@yahoo.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
I need some help writing this function

the function returns a list of random non-duplicate integers in a string,
1 parameter indicating the maximum range.

string randGen(int maxRange)

maxRange=1000
the output would be:

total of 1000 numbers from 1 - 1000 randomly
2,6,3,423,123,673,23,53...

I know how to write the random number generator part but im having trouble
with the non-duplicate part.
any help is appreciated.

Aaron

Jul 21 '05 #2
If I have understood you correctly ? - Quick and dirty (untested) Pseudo
code . . .
Declare your Array to store the 1000 random numbers
Generate a random seed ( you may use time as an option )
Declare your Random Number using range and seed
Generate a new Random number in my range

While I have not reached the total number of random elements

If this number is not in my current list, add it, else get the
next random number

End the loop

Convert the Array to a string and Return the String containing the random
elements.

Basic. But this one way of approaching it

HTH
Terry Burns

http://TrainingOn.net

"Aaron" <ku*****@yahoo.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
I need some help writing this function

the function returns a list of random non-duplicate integers in a string,
1 parameter indicating the maximum range.

string randGen(int maxRange)

maxRange=1000
the output would be:

total of 1000 numbers from 1 - 1000 randomly
2,6,3,423,123,673,23,53...

I know how to write the random number generator part but im having trouble
with the non-duplicate part.
any help is appreciated.

Aaron

Jul 21 '05 #3
On Sun, 17 Apr 2005 10:37:38 -0700, "Aaron" <ku*****@yahoo.com> wrote:
I need some help writing this function

the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.

string randGen(int maxRange)

maxRange=1000
the output would be:

total of 1000 numbers from 1 - 1000 randomly
2,6,3,423,123,673,23,53...

I know how to write the random number generator part but im having trouble
with the non-duplicate part.
any help is appreciated.

Aaron

This has the distinct look of homework.

Imagine you have a deck of 1000 cards numbered 1 to 1000.

Pick a card at random and put it in a new pile. Next pick another
card at random from the remaining 999 cards and add it to the new
pile. Repeat until all 1000 cards are in the new pile. The new pile
solves the problem.

For extra credit this can be done without using extra storage. (Hint:
there are never more than 1000 cards in total in both piles.)

Goggle for "Knuth shuffle algorithm" if you want more detail.

rossum

The ultimate truth is that there is no ultimate truth
Jul 21 '05 #4
If you present this solution, be prepared to explain why it works (and
probably convert it to C# from VB, judging from the list of ngs you've
posted to):-

Dim a As System.Collections.arraylist = New System.Collections.arraylist
Dim n As Integer = 10
Dim i As Integer
For i = 0 To n - 1
a.Insert(CInt(Rnd() * i), i+1)
Next

Andrew
Jul 21 '05 #5
Andrew,

We have in the dotNet newsgroups a kind of etiquette that we help students
however never with code.

Just to let you know,

Cor
Jul 21 '05 #6
> Andrew,

We have in the dotNet newsgroups a kind of etiquette that we help students
however never with code.

Just to let you know,

Cor

Thanks for the info - I'll have to set my "looks likes homework" theshold
lower in the .net newsgroups. I just thought that the method I presented was
interesting because... well, I can't say because that'd explain the code :-)

Andrew
Jul 21 '05 #7

if i understand this correctly a[0] is never used.

what is the difference between a regular array and an arraylist? arraylist
seems to have more features. but is it lower performance-wise?
"Andrew Morton" <ak*@in-press.co.uk.invalid> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
If you present this solution, be prepared to explain why it works (and
probably convert it to C# from VB, judging from the list of ngs you've
posted to):-

Dim a As System.Collections.arraylist = New System.Collections.arraylist
Dim n As Integer = 10
Dim i As Integer
For i = 0 To n - 1
a.Insert(CInt(Rnd() * i), i+1)
Next

Andrew

Jul 21 '05 #8
Aaron <ku*****@yahoo.com> wrote:

if i understand this correctly a[0] is never used.
Nope, you've missed something. a[0] would be used if the random number
generator ever returned 0 (which it's bound to on the first iteration,
for instance.) The first parameter of Insert is the position, the
second is the value.
what is the difference between a regular array and an arraylist? arraylist
seems to have more features. but is it lower performance-wise?

There's a slight hit, but not a lot. The big difference is that
ArrayLists transparently resize (at some performance cost when they do
so) whereas arrays themselves never change size.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet