473,405 Members | 2,154 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,405 software developers and data experts.

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
Nov 17 '05 #1
8 2163
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

Nov 17 '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

Nov 17 '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
Nov 17 '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
Nov 17 '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
Nov 17 '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
Nov 17 '05 #7
thanks for all your responses.

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

Nov 17 '05 #8
Aaron <ku*****@yahoo.com> wrote:
thanks for all your responses.

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
If replying to the group, please do not mail me too
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.