473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Number Generation

Hi everybody,

May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?

Thanks in advance!

May 18 '06 #1
10 2394
muttaa said:
Hi everybody,

May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?


One common technique is the Linear Congruential PseudoRandom Number
Generator. This works on the principle of taking a "current" pseudo-random
value r and three constants, a, c, and m, and producing the next
pseudo-random number by this method:

r = (a * r + c) % m;

How good this generator is depends largely on your choices for a, c, and m
(and, of course, other factors, such as the range of r).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 18 '06 #2
>May i know how to write a code that generates random numbers as many
times as one would want ?
Code does not generate random numbers. Code generates pseudo-random
numbers. For real random numbers you need hardware.

Any pseudo-random number generator will repeat itself eventually, even
if it takes longer than the expected lifetime of the universe.
i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?


Google for "mersenne twister". Distributions for Linux and FreeBSD
also contain source code for various pseudo-random number generators
like random() and srand48().

Gordon L. Burditt
May 18 '06 #3
Gordon Burditt said:
May i know how to write a code that generates random numbers as many
times as one would want ?


Code does not generate random numbers. Code generates pseudo-random
numbers. For real random numbers you need hardware.


Or wetware. You can ask users to supply real random numbers, and use those.
Of course, they might just give you 1, 2, 3, 4, 5..., but that's a QofI
issue!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 18 '06 #4
"muttaa" <as***********@ gmail.com> writes:
May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?


The comp.lang.c FAQ is at <http://www.c-faq.com/>. Question 13.15 is
"I need a random number generator."; questions 13.16 through 13.21
also deal with random numbers.

Start there, and come back if you still have questions.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 18 '06 #5
muttaa wrote:
Hi everybody,

May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?

Thanks in advance!


Google for Marsaglia.

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
May 19 '06 #6
Richard Heathfield wrote:
muttaa said:
Hi everybody,

May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?


One common technique is the Linear Congruential PseudoRandom Number
Generator. This works on the principle of taking a "current" pseudo-random
value r and three constants, a, c, and m, and producing the next
pseudo-random number by this method:

r = (a * r + c) % m;

How good this generator is depends largely on your choices for a, c, and m
(and, of course, other factors, such as the range of r).

interesting.
m would be rand_max-1 i think
a and c would be prime numbers maybe? (well, that was my choice)
and initial value of r would be the seed.
I played with this once i saw your post and it seems quite workable
as a pseudo random generator. Its also extremely easy to implement.
I love little tidbits like this!
Eric

May 20 '06 #7


Eric wrote On 05/19/06 20:40,:
Richard Heathfield wrote:

muttaa said:

Hi everybody,

May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?

One common technique is the Linear Congruential PseudoRandom Number
Generator. This works on the principle of taking a "current" pseudo-random
value r and three constants, a, c, and m, and producing the next
pseudo-random number by this method:

r = (a * r + c) % m;

How good this generator is depends largely on your choices for a, c, and m
(and, of course, other factors, such as the range of r).


interesting.
m would be rand_max-1 i think
a and c would be prime numbers maybe? (well, that was my choice)
and initial value of r would be the seed.
I played with this once i saw your post and it seems quite workable
as a pseudo random generator. Its also extremely easy to implement.
I love little tidbits like this!


From your speculations about appropriate values for
m, a, and c, it is clear that you have no knowledge at
all about what they should be. You therefore deserve
the Bad Things that are likely to happen to you if you
decide to implement any such "little tidbit." Alas, it
is only too likely that you will code it into a program
someone else will use, thus inflicting your ignorance
upon innocent bystanders.

If you can't be bothered to do any research at all,
there's no hope. But in hope you can be persuaded to
lift a finger five times and make a few mouse clicks,
I offer one word: RANDU.

--
Er*********@sun .com

May 22 '06 #8
Eric Sosman wrote:
Eric wrote On 05/19/06 20:40,:
Richard Heathfield wrote:
muttaa said:

May i know how to write a code that generates random numbers as
many times as one would want ? i.e. like the standard function
'rand()' or may i get the basic logic behind it ?

One common technique is the Linear Congruential PseudoRandom
Number Generator. This works on the principle of taking a
"current" pseudo-random value r and three constants, a, c, and m,
and producing the next pseudo-random number by this method:

r = (a * r + c) % m;

How good this generator is depends largely on your choices for a,
c, and m (and, of course, other factors, such as the range of r).


interesting.
m would be rand_max-1 i think
a and c would be prime numbers maybe? (well, that was my choice)
and initial value of r would be the seed.
I played with this once i saw your post and it seems quite workable
as a pseudo random generator. Its also extremely easy to implement.
I love little tidbits like this!


From your speculations about appropriate values for
m, a, and c, it is clear that you have no knowledge at
all about what they should be. You therefore deserve
the Bad Things that are likely to happen to you if you
decide to implement any such "little tidbit." Alas, it
is only too likely that you will code it into a program
someone else will use, thus inflicting your ignorance
upon innocent bystanders.

If you can't be bothered to do any research at all,
there's no hope. But in hope you can be persuaded to
lift a finger five times and make a few mouse clicks,
I offer one word: RANDU.


A better word might be TAOCP.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
May 23 '06 #9
Eric Sosman wrote:


Eric wrote On 05/19/06 20:40,:
Richard Heathfield wrote:

muttaa said:
Hi everybody,

May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?
One common technique is the Linear Congruential PseudoRandom Number
Generator. This works on the principle of taking a "current"
pseudo-random value r and three constants, a, c, and m, and producing the
next pseudo-random number by this method:

r = (a * r + c) % m;

How good this generator is depends largely on your choices for a, c, and
m (and, of course, other factors, such as the range of r).


interesting.
m would be rand_max-1 i think
a and c would be prime numbers maybe? (well, that was my choice)
and initial value of r would be the seed.
I played with this once i saw your post and it seems quite workable
as a pseudo random generator. Its also extremely easy to implement.
I love little tidbits like this!


From your speculations about appropriate values for
m, a, and c, it is clear that you have no knowledge at
all about what they should be. You therefore deserve
the Bad Things that are likely to happen to you if you
decide to implement any such "little tidbit." Alas, it
is only too likely that you will code it into a program
someone else will use, thus inflicting your ignorance
upon innocent bystanders.

If you can't be bothered to do any research at all,
there's no hope. But in hope you can be persuaded to
lift a finger five times and make a few mouse clicks,
I offer one word: RANDU.

Jesus Man, take a fucking chill pill will ya? Its not the end of the world
you know.
Eric

May 31 '06 #10

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

Similar topics

10
11946
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
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
10
2889
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 have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there...
10
741
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of rand(): #include <stdlib.h> #include <time.h>
13
4224
by: quickcur | last post by:
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
4
4110
by: Dimos | last post by:
Hello All, I need some help with random number generation. What I need exactly is: To create a few thousand numbers, decimal and integers, between 5 and 90, and then to export them as a single column at a spreadsheet.
22
3430
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
21
13501
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
8
7553
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
16
539
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers given a seed. - Relatively uniform, does not have to be perfect. The application is not a security or statistics application, the quality of numbers is not a priority although a fairly uniform
0
8425
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
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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
8622
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
7355
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...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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

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.