473,624 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple random number

Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)

Thanks,

qq

Nov 15 '05 #1
13 4222
qu******@yahoo. com writes:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)


The fastest way would probably be to use a function other than
rand() to do the generation. Other than that, I don't even see
what question you're asking. There are only a few reasonable
ways to write a 10-iteration loop, given no more information.
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
Nov 15 '05 #2
qu******@yahoo. com wrote:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)

Use a different rand() that is not expensive, possibly taking into
account a loss of randomness.

A random number generator based on linear congruence is fast and, if
written well, "random enough" for most purposes. You could seed it with
one number from rand(), if this is a superior source of random numbers.

See question 13.15 of the FAQ of this ng. You may also get better
replies in a newsgroup that's not specific to C, like comp.programmin g
and sci.crypt.rando m-numbers (but read their FAQs first, when
appropriate, I have not).

(Pseudo)-random number generation is its own field of study in computer
programming.

S.
Nov 15 '05 #3
Skarmander wrote:
qu******@yahoo. com wrote:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)

Use a different rand() that is not expensive, possibly taking into
account a loss of randomness.

A random number generator based on linear congruence is fast and, if
written well, "random enough" for most purposes. You could seed it with
one number from rand(), if this is a superior source of random numbers.

Update to my own reply: a popular fast RNG that is rumored to outpace
even LC while having much better statistical properties is the Mersenne
Twister (http://www.math.sci.hiroshima-u.ac.j...t/MT/emt.html),
which comes with C source.

S.
Nov 15 '05 #4
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
<qu******@yahoo .com> wrote:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)


That's an algorithm question, not a C question.

[OT]
If rand() really does return a *random* number, then you cannot
do what you want with just 10 calls to rand(). If rand() is truly
producing a random number, then it could *by chance* return 0
four hundred and twenty-two times in a row -- and then turn
around and return 100 for the next 3 million calls. This circumstance
is merely -unlikely-, not impossible.

If you try to overcome this by using some kind of algorithm to
choose a different number (e.g., md5 hash of (rand() + the current index)
then anyone who knew your algorithm and knew the previous numbers
in the series would be able to predict the next number in the series
with probability greater than that inherent in the random
distribution function.

So really you cannot do much except to keep calling rand() until you
have 10 different results. If, as you say, rand() is expensive,
then nearly any reasonable kind of housekeeping to check for duplicates
would be less expensive than a single call to rand().
--
Chocolate is "more than a food but less than a drug" -- RJ Huxtable
Nov 15 '05 #5
<qu******@yahoo .com> wrote:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)


A series of random numbers may include repeated numbers. So you are asking
for a random number that *isn't* a random number.

What is that thing in parenthesis at the end? Is that a tentative solution?
Why is it in parenthesis? Clearly it is *not* a usable solution to the
question you ask.

I haven't read and understood all the messages in this thread so this may be
a repeat. I would draw ten random numbers, put them in an array, copy the
array, sort the duplicate array, and look for duplicate entries. Replacing
and retesting if needed. To use the numbers, just draw them in order, 0, 1,
2 .... in the originally drawn array. After all they are already random,
there is no reason to redo something.
Nov 15 '05 #6
qu******@yahoo. com wrote:
# Suppose I have a function rand() that can generate one integer random
# number between 0 and 100. Suppose also rand() is very expensive. What
# is the fastest way to generate 10 different random number between 0 and
# 100? (call rand() only 10 times...)

If you want a random draw, you can check Knuth's Art of Programming.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Raining down sulphur is like an endurance trial, man. Genocide is the
most exhausting activity one can engage in. Next to soccer.
Nov 15 '05 #7
osmium wrote:
<qu******@yahoo .com> wrote:

Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)

A series of random numbers may include repeated numbers. So you are asking
for a random number that *isn't* a random number.

What is that thing in parenthesis at the end? Is that a tentative solution?
Why is it in parenthesis? Clearly it is *not* a usable solution to the
question you ask.

I haven't read and understood all the messages in this thread so this may be
a repeat. I would draw ten random numbers, put them in an array, copy the
array, sort the duplicate array, and look for duplicate entries. Replacing
and retesting if needed.


Actually, for generating just 10 numbers, doing it the "stupid" way is
probably faster than going through all that.

int numbers[count];
int i;
do {
for (i = 0; i != count; ++i) {
int j;
numbers[i] = rand(); /* Some rand()-based expression */
for (j = 0; j != i && numbers[j] != numbers[i]; ++j);
if (j != i) break;
}
} while (i != count);

That is, just do a linear search for duplicates when you generate a new
value. This doesn't scale, but is efficient for small values of count.

Here's the version for the goto appreciation society, with C99 style
declarations:

int numbers[count];
restart:
for (int i = 0; i != count; ++i) {
numbers[i] = rand();
for (int j = 0; j != i; ++j) {
if (numbers[j] == numbers[i]) goto restart;
}
}

S.
Nov 15 '05 #8
On Sat, 12 Nov 2005 02:15:32 +0100, Skarmander wrote:
[generating 10 unique random numbers]
Actually, for generating just 10 numbers, doing it the "stupid" way is
probably faster than going through all that.

int numbers[count];
int i;
do {
for (i = 0; i != count; ++i) {
int j;
numbers[i] = rand(); /* Some rand()-based expression */
for (j = 0; j != i && numbers[j] != numbers[i]; ++j);
if (j != i) break;
}
} while (i != count);

That is, just do a linear search for duplicates when you generate a new
value. This doesn't scale, but is efficient for small values of count.
More so if you reverse the outermost and middle loops (modifying
conditionals).
Here's the version for the goto appreciation society, with C99 style
declarations:

int numbers[count];
restart:
for (int i = 0; i != count; ++i) {
numbers[i] = rand();
for (int j = 0; j != i; ++j) {
if (numbers[j] == numbers[i]) goto restart;
}
}


This one's easier - just bring restart down a line.

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #9
quick...@yahoo. com wrote:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number between 0 and
100? (call rand() only 10 times...)


rand() is a standard C function call. Its range is between 0 and
RAND_MAX inclusive, and RAND_MAX must be >= 32767, but is defined by
your compiler. Anyhow, so suppose that instead we have rand10() which
behaves as the rand() function you described.

Ok, you don't specify any qualifications to the kind of "random" that
you want, so if you produce 10 random numbers between 0 and 10, then
they will also be between 0 and 100, so you're done. If you are
expecting more, like that there is a chance that any number between 0
and 100 is chosen, then you can do the obvious:

a[0] = rand10();
for(i=1; i < 10; i++) a[i] = a[i-1] + rand10();

The numbers won't be random with respect to each other, but taken as a
whole, you could sort of say these number are "random" and covers your
range. Now you have to understand that unlike 10 choices from U[0,100]
(meaning uniformly distributed from 0 to 100 inclusive, and in this
case, just picking integers) there are many anomolies with such a
method of choice -- no gap of more than 10 is possible, the [0,10] gap
is not possible, and the expected average is far far less than 50 (I
think its 27.5, but don't quote me). Also taken as a sequence, the
numbers are also always non-decreasing (but this may not matter to
you.)

If you want to actually generate 10 random numbers from the U[0,100]
distribution, well, it seems you simply don't have enough entropy from
just ten choices from U[0,10]. In general entropy cannot be increased
without additional sources of entropy.

--
Paul Hsieh
http://www.pobox.com/~qed/random.html
http://bstring.sf.net/

Nov 15 '05 #10

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

Similar topics

10
2494
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
5
3339
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 double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
19
3054
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A unique 'string' is then generated using itoa() and rand() for each block of memory. Finally using pointer-to-pointer of type char, I print the contents of the blocks of memory, i.e. the strings, using both printf() and manually, character by...
9
6078
by: toton | last post by:
Hi, I am looking for a circular buffer solution ( a fixed buffer) , where the elements can be pushed back & removed front. It looks stl deque can be a solution. my question is, 1) circular buffer is random access? (i.e implemented over array and not linked list) 2) if I reserve the required space & do push_back & remove_front so that total memory doesn't exceed the reserved amount, will the internal pointers will wrap, or the memory...
2
3640
by: HumanJHawkins | last post by:
I wrote this question a little differently in the MFC group since it is a bit of a different environment than pure C++, but I hope you will forgive the similarities if anyone is reading both groups. Basically, I need to seed the random number generator in 4 seperate threads at once. So, I can't use the current time as the random seed, because then all four threads end up with the same series of (therefore non-random) numbers. Is there...
4
10561
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work....
13
2798
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
10
4494
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more or less, a taskId. In this case, the userSuppliedState {really taskId} is of the object type,...
7
2270
by: wozza | last post by:
hi I'm a Dreamweaver user who's created a few simple data entry/ registrations forms in my time, but I'm not really a coder (though I can follow instructions and am not afraid to dabble...) - I generally make use of DW's builtin commands and some extensions. Anyway I'm creating a registration site for event exhibitors and I've been asked to come up with a method of automatically generating passwords for inserted records, either as...
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.