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

rand function

bob
what algorithm does the rand function use?

Apr 22 '06 #1
3 4829
bo*@coolgroups.com wrote:
what algorithm does the rand function use?

Read the man, info or whatever page and find out!

--
Ian Collins.
Apr 22 '06 #2

<bo*@coolgroups.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
what algorithm does the rand function use?


The algorithm isn't specified in the standard (the C standard in this case).
Note that it is only required to deliver 16 bits. See the value of RAND_MAX
for your compiler.
Apr 23 '06 #3
<bo*@coolgroups.com> wrote:
what algorithm does the rand function use?


As mentioned elsewhere, whatever the deigner chooses to use.

The link below suggests:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

is at least acceptable. If one wanted a quick and dirty generator with
uniform results across different platforms, one might use that. Calling
rand() will likely give different results with different compilers. I
recall seeing that 12345 in a snippet elsewhere on the net, too.

http://cer.freeshell.org/renma/
Apr 23 '06 #4

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

Similar topics

4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
7
by: eyal.susser | last post by:
I hear rand() is not thread safe. I was using it, foolish man that I am. But what is meant exactly by unsafe? What can happen? Bizzare results from rand()? Something worse? Thanks, Eyal.
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
0
by: Skybuck Flying | last post by:
I found some rand source code on this link: http://www.codeguru.com/forum/showthread.php?t=312416&goto=nextnewest void __cdecl srand (unsigned int seed) { #ifdef _MT _getptd()->_holdrand =...
5
by: vib | last post by:
Hi there, i) Is there any other way to replace the rand()of standard C lib? ii) How efficient is the rand()? Thanks in advance. vib
10
by: Frank Silvermann | last post by:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define MIN_WORD_LENGTH 9
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
13
by: Spiros Bousbouras | last post by:
The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if...
7
by: john_smith_1221 | last post by:
Hello, I need to use the rand() function to generate a random value, I already know how to do it with srand(time(NULL)) and its "randomness" is sufficient for me, the problem is my code requires to...
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.