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

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_MAX);
}

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 3653
"Peter Williams" <pe*@bigpond.net.au> writes:
I need to port the following (small) C language function to Delphi:

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

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_Keith) 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.net.au> writes:
double Rand(void) {
return rand()/(1.0 + (double)RAND_MAX);
}

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
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...
10
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...
0
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
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...
6
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
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...
8
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...
3
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...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.