473,395 Members | 1,915 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,395 software developers and data experts.

random numbers..

hello.

I want to write my own random number generator..can anyone help me how to approach the problem??
Jul 26 '07 #1
20 2143
Meetee
931 Expert Mod 512MB
hello.

I want to write my own random number generator..can anyone help me how to approach the problem??
Hi,

Have you searched on google? Also see this

Kindly paste what you have done so far. Then only members can help you.

Regards
Jul 26 '07 #2
ravenspoint
111 100+
zodilla58: He doesn't have any code to post - he doesn't know how to begin. If you want to be a policeman, go somewhere else.

OP: The first question you have to ask is, what kind of random numbers do you want?

There are real random numbers, which no-one can predict. They have to be obtained from some physical process, like tossing a coin and storing the results, or looking at the least significant bit of a clock.

Or there are psuedo random numbers, which are perfectly predictable but seem to nbe random at a glance. These are generated using a mathematical function.
Jul 26 '07 #3
If you include the standard library there is the function random(someInteger), that is going to return an integer from 0 to someInteger.

I believe it's the second type of random function generator that ravenspoint posted (mathematical).

Ras.
Jul 26 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
zodilla58: He doesn't have any code to post - he doesn't know how to begin. If you want to be a policeman, go somewhere else.
ravenspoint

zodilla58 is only operating under the guidelines of this site. We don't believe in spoonfeeding answers to posters. We are here to help them learn. Asking for clarification from a poster including what code, etc. they have already attempted is standard practice.

Also if there is a belief that the poster is asking homework or coursework question then there are very clear guidelines as to how these questions should be answered. One of which is that full code answers should not be posted in answer to these questions.

Also I would be grateful if you didn't make personal remarks to other members about how they should or shouldn't answer a post. If you have a problem with how another member deals with a post then raise the issue politely or send a PM to a Moderator / Admin.

This is a technical community of developers and in general we get along with very little conflict.

Mary
ADMIN
Jul 26 '07 #5
r035198x
13,262 8TB
zodilla58: ... If you want to be a policeman, go somewhere else.

...
I don't think that line was neccessary.
There's nothing wrong with zody's response. There was no way that zody could have guessed whether the OP had any code or not and so asking for the OP's code was not wrong.
I hope zody was not offended with that, and of course you'll try not to offend others as well in the future, eh, ravenspoint.
Jul 26 '07 #6
r035198x
13,262 8TB
ravenspoint

zodilla58 is only operating under the guidelines of this site. We don't believe in spoonfeeding answers to posters. We are here to help them learn. Asking for clarification from a poster including what code, etc. they have already attempted is standard practice.

Also if there is a belief that the poster is asking homework or coursework question then there are very clear guidelines as to how these questions should be answered. One of which is that full code answers should not be posted in answer to these questions.

Also I would be grateful if you didn't make personal remarks to other members about how they should or shouldn't answer a post. If you have a problem with how another member deals with a post then raise the issue politely or send a PM to a Moderator / Admin.

This is a technical community of developers and in general we get along with very little conflict.

Mary
ADMIN
Since when did you get a faster keyboard than mine?
Jul 26 '07 #7
Banfa
9,065 Expert Mod 8TB
I want to write my own random number generator..can anyone help me how to approach the problem??
Step 1, research the topic, this is fairly easy to do, for a topic like random and pseudo random numbers I would start on Wikipedia.

Step 2, choose an algorthim, also not particularly hard as you will find when you look round Wikipedia that there is a plethera of information available and many of the algoithms have pseudo coded examples.

Step 3, Implement the algorthim probably the hardest part but may still be quite easy. Take your pseudo coded algorithm, convert it to compilable C/C++ code. Post here if you run into difficulties.
Jul 26 '07 #8
Step 1, research the topic, this is fairly easy to do, for a topic like random and pseudo random numbers I would start on Wikipedia.

Step 2, choose an algorthim, also not particularly hard as you will find when you look round Wikipedia that there is a plethera of information available and many of the algoithms have pseudo coded examples.

Step 3, Implement the algorthim probably the hardest part but may still be quite easy. Take your pseudo coded algorithm, convert it to compilable C/C++ code. Post here if you run into difficulties.
This is some pretty good answer. But sometimes I wonder if it's actually worth it to answer such posts and spend much more energy than the initial poster seems to have put.

Ras.
Jul 27 '07 #9
Banfa
9,065 Expert Mod 8TB
This is some pretty good answer. But sometimes I wonder if it's actually worth it to answer such posts and spend much more energy than the initial poster seems to have put.
If they learn the methodology of how to set about tackling a problem and can apply it to their next problem then it certainly is. Not being clairevoyant I have to assume that they might learn if told however they certainly can not learn if not told so I tell'em and hope for the best.

Additionally while looking up information related to their problem there is always the chance that I will come accros something interesting that I was unaware of before so I benefit too.

And finally this is in fact a very inteligent question, the OP has not asked us to solve his actual problem (writing a random number generator), they have asked us how they can solve their own problem and is therefore definately worthy of an answer.
Jul 27 '07 #10
If you include the standard library there is the function random(someInteger), that is going to return an integer from 0 to someInteger.

I believe it's the second type of random function generator that ravenspoint posted (mathematical).

Ras.
The standard random number generator isn't very good. I would not suggest ever using it.

The best way to create a list of random numbers is to use a rng, unless you have a radioactive sample lying around andf you want to count the times between decays. The simplest method of generating random numbers is to use what's known as the Linear Congruential Generator (LCG). This will create a sequence of uniformly distributed random numbers.

As pointed out, this is not, strictly speaking, a set of truly random numbers. This numbers will become periodic with period 2^{m}.

The equation which defines the sequence is given by

x_{n} = a x_{n-1} + b mod M

To have numbers which are periodic with period 2^{m} define the parameters as follows

a = 1 mod 4
b = 1 mod 2
M = 2^{m}

m is any number natural number. Starting with the seed x_{0}, a sequence of random numbers can be generated. The larger the value for m the better because you will not have repition. Other choices for a, b and M exist but these should get you started.
Jul 27 '07 #11
ravenspoint
111 100+
The standard random number generator isn't very good. I would not suggest ever using it.
That is so harsh!

In fact the standard generator is free, widely available and well understood. These are important benefits not shared by a home brewed generator. They are so important benefits, that I would suggest using the standard generator, unless someone can show a very good reason why it would be the wrong choice.

If you routinely reseed the generator from the least significant digits of the system clock, then the standard generator serves perfectly for a huge number of the common purposes.

The classic example is selecting a wait time before again requesting a shared resource that is currently busy. It is does not matter what wait time is selected, just that it be 'random'. Obscure flaws in the statistical behaviour of the standard generator over long runs are irrelevant - if you have to back-off more than 2 - 3 times your system has problems that will not be solved with a 'better' generator.
Jul 27 '07 #12
JosAH
11,448 Expert 8TB
To add to Emaghero's very fine post: for a bit of theory behind this all have a look
at Donald Knuth's "The Art Of Computer Programming".

If I'm not mistaken it's vol 2 "Seminumerical Algorithms" where all this is shown
and proven (there are quite a few more pseudo random number generators in
there too).

kind regards,

Jos
Jul 27 '07 #13
JosAH
11,448 Expert 8TB
That is so harsh!

In fact the standard generator is free, widely available and well understood. These are important benefits not shared by a home brewed generator. They are so important benefits, that I would suggest using the standard generator, unless someone can show a very good reason why it would be the wrong choice.

If you routinely reseed the generator from the least significant digits of the system clock, then the standard generator serves perfectly for a huge number of the common purposes.
'common purposes' often go beyond a simple 'getting a number for which you
don't understand how your PC got it'. Reseeding the 'standard' pseudo random
number generator severly breaks its uniform distribution; it is not an option for
a lot of 'common purposes'.

Have a look at GNU's implementation for a much better pseudo random number
generator; it is free also and considered quite good; much better than the
'standard' implementation, widely used and available and well understood by
those who have a 'common use' for it.

kind regards,

Jos
Jul 27 '07 #14
ravenspoint
111 100+
By free, I did not mean that you did not have to write a check.

I meant that you do not have to select an generator, install it, link it to your program, distribute it to your clients, and document why you chose it. This is all a huge amount of work for anyone who is not paid by the hour.

The standard generator is just there, free.
Jul 27 '07 #15
r035198x
13,262 8TB
By free, I did not mean that you did not have to write a check.

I meant that you do not have to select an generator, install it, link it to your program, distribute it to your clients, and document why you chose it. This is all a huge amount of work for anyone who is not paid by the hour.

The standard generator is just there, free.
I'd say the right tool for the right job. the standard generator is for testing small programs and writting school assignments. If you use it for a lotto system however, you better be loaded with cash.
Jul 27 '07 #16
ravenspoint
111 100+
I'd say the right tool for the right job. the standard generator is for testing small programs and writting school assignments. If you use it for a lotto system however, you better be loaded with cash.
Granted.

However, most coding jobs fall between these extremes. Quick and dirty solutions to small problems some-one is prepared to pay a hundred bucks to fix.

Coding for a lottery system would be a grand job.
Jul 27 '07 #17
r035198x
13,262 8TB
Granted.

However, most coding jobs fall between these extremes. Quick and dirty solutions to small problems some-one is prepared to pay a hundred bucks to fix.

Coding for a lottery system would be a grand job.
Some of these "small" things also differentiate between a good programmer and one who only cares about this month's salary.
Jul 27 '07 #18
JosAH
11,448 Expert 8TB
Coding for a lottery system would be a grand job.
No it wouldn't; it would just take a fairly good pseudo random number generator,
capable of generating a reasonable length series of uniform distributed pseudo
random numbers. The 'standard' rand() isn't one of them but that's all.

Jos
Jul 27 '07 #19
MMcCarthy
14,534 Expert Mod 8TB
The last three posts in this thread have been deleted as they had turned into a personal conflict and hijacked the thread. This does not in any way help the original poster and is not allowed in the development forums.

Please remember the OP when discussing solutions in a thread.

ADMIN
Jul 27 '07 #20
That is so harsh!

In fact the standard generator is free, widely available and well understood. These are important benefits not shared by a home brewed generator. They are so important benefits, that I would suggest using the standard generator, unless someone can show a very good reason why it would be the wrong choice.

If you routinely reseed the generator from the least significant digits of the system clock, then the standard generator serves perfectly for a huge number of the common purposes.
If you have any kind of serious numerical application, such as Monte-Carlo Integration or Stochastic Differential Equations to name two, then the standard generator is hopelessly inadequate. This is because these processes usually require a specific type of random number. Physical processes modelled by SDEs have solutions based on gaussian random variables. To determine the price of a stock option at any given time one must use log-normal random variables. While these are specialised applications they are areas where random numbers are used heavily in practice and that standard generator is not used because of the necessity to continuously re-seed.

The classic example is selecting a wait time before again requesting a shared resource that is currently busy. It is does not matter what wait time is selected, just that it be 'random'. Obscure flaws in the statistical behaviour of the standard generator over long runs are irrelevant - if you have to back-off more than 2 - 3 times your system has problems that will not be solved with a 'better' generator.
The queuing times for many systems can be modelled very accurately with Poisson statistics. I am not saying that you should use "obscure" random variables for the sake of it. I am saying that certain applications require certain types of random variables and in those cases an application specific random number generator is required.
Jul 30 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
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: 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,...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.