473,886 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert C code to Delphi -- random function

Hi All,

I need to port the following (small) C language function to Delphi:

double Rand(void) {
return rand()/(1.0 + (double)RAND_MA X);
}

NB -- elsewhere in the program randomize is called before-hand.

I don't know what (exactly) the C language function "rand()" does.
Also, RAND_MAX is not defined anywhere in the program, so is it part of
C ???

My attempt at starting to write it in Delphi:

function Rand : Real;
begin
Result := ??????????????? ?
end;

NB -- Delphi has a Random(N) function which takes an integer (N) as
it's parameter and returns an integer in the range 0..N-1 . E.g.
Random(3) would return either 0, 1, or 2.

NB(2) -- Obviously, Delphi also has a "randomize" function which I am
aware I need to call first.

Can anyone please assist me in writing this routine as a Delphi code
snippet???

Any help will be greatly appreciated. :-)

Regards,
Peter W. :-)))
Sandy Bay, Hobart, Tas, AU.
Nov 14 '05 #1
2 3676
"Peter Williams" <pe*@bigpond.ne t.au> writes:
I need to port the following (small) C language function to Delphi:

double Rand(void) {
return rand()/(1.0 + (double)RAND_MA X);
}

NB -- elsewhere in the program randomize is called before-hand.

I don't know what (exactly) the C language function "rand()" does.
Also, RAND_MAX is not defined anywhere in the program, so is it part of
C ???
You should be able to find this information in any C textbook or in
your system's documentation, but ...

The rand() function returns a pseudo-random integer in the range 0 to
RAND_MAX.

RAND_MAX is an integer constant defined in <stdlib.h>; its value will
vary from one implementation to another, but it's guaranteed to be at
least 32767.

srand() seeds the random number generator. A given seed will produce
a repeatable sequence of numbers from rand(). If you don't call
srand(), it's equivalent to calling it with a seed value of 1.

The function appears to be attempting to generate a random double
(floating-point) number in the range 0.0 to 1.0, with the upper bound
excluded.

You might take a look at section 13 of the C FAQ,
<http://www.eskimo.com/~scs/C-faq/top.html>.
My attempt at starting to write it in Delphi:

[snip]

Good luck with the Delphi part; we can't help you with it here.

--
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.
Nov 14 '05 #2
Keith Thompson wrote:

"Peter Williams" <pe*@bigpond.ne t.au> writes:
double Rand(void) {
return rand()/(1.0 + (double)RAND_MA X);
}

The function appears to be attempting to generate a random double
(floating-point) number in the range 0.0 to 1.0, with the upper bound
excluded.


And the cast is superfluous.

--
pete
Nov 14 '05 #3

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

Similar topics

4
5400
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
10
2513
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
0
1692
by: W. D. | last post by:
Hi Folks, My Delphi is rusty, and my C is corroded. Am I understanding this properly? CPP Code: TMainForm *MainForm Is this a declaration for a form named 'MainForm'
4
1928
by: Garry Freemyer | last post by:
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely flustered over this one because I'm a little rusty and have been struggling over this for days, not wanting to show all my ignorance in final desparate plea for help which this is ... Here is the macro ... #define X(t,b) (sp->x)
6
2803
by: Kostya Ergin | last post by:
hi, i am very new in C#. How to do this in C#: This is function: function test(param: string): string; begin end; I can get result of "test" function:
8
1532
by: GabrielESandoval | last post by:
i currently use the code below to create a slideshow of images. i edited it so that its not as long. i currently have over 20 images i want to change it so that the images dont appear in the same order each time. what would i have to change to make the cycle random??? thanks in advance for the help. gabriel -- heres the code --
8
6678
by: mesutalturk | last post by:
How do i convert the following C# code to Delphi? public static uint GenerateSiteID(string SiteName) { uint id = 0; char arr = SiteName.ToCharArray(); //Convert the sitename to a Char Array for (int i = 0; i < arr.Length; i++) {
3
2755
by: SpreadTooThin | last post by:
I am wondering if someone who knows the implemention of python's time could help converting this to c/c++.... nanoseconds = int(time.time() * 1e9) # 0x01b21dd213814000 is the number of 100-ns intervals between the # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. self.timestamp = int(nanoseconds/100) + 0x01b21dd213814000L self.clock_seq = random.randrange(1<<14L) # instead of stable storage
2
3102
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See the section on "simple decimal...
0
10770
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
10872
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
10429
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
9592
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
7987
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
7139
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
6010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4627
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
3
3245
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.