472,984 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 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
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
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

Jul 21 '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
Jul 21 '05 #9

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

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
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...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.