Hello everyone,I don't speak very good english , so sorry if i make any mistake
Can anyone explain me this code
[PHP]unsigned int rand_seed = 1;
void srand(unsigned int seed) {
rand_seed = seed;
}
int rand(void) { /*i don't understand what code do here down*/
return ((rand_seed = rand_seed * 1103515245 + 12345 ) >> 16) & 0x7fff;
}[/PHP]
I know that if we want to change the seed with time, we write [PHP]srand((int)time(NULL))[/PHP]
ps:If this thread is not in the right section please move it
Thanks in advance
4 6594
Hello everyone,I don't speak very good english , so sorry if i make any mistake
Can anyone explain me this code
[PHP]unsigned int rand_seed = 1;
void srand(unsigned int seed) {
rand_seed = seed;
}
int rand(void) { /*i don't understand what code do here down*/
return ((rand_seed = rand_seed * 1103515245 + 12345 ) >> 16) & 0x7fff;
}[/PHP]
I know that if we want to change the seed with time, we write [PHP]srand((int)time(NULL))[/PHP]
ps:If this thread is not in the right section please move it
Thanks in advance
It's the right forum, no need to worry. You were looking at the source code for
the rand() pseudo random number generator. It's a linear congruence method.
Read all about that method here.
kind regards,
Jos
So if understand good ,it's a method to generate random numders?
In the link that you gave me has a type about this method with X ,c and others.So in my case .We choose some numbers like 1103515245.How this choice made? maybe depends on computer?
So if understand good ,it's a method to generate random numders?
In the link that you gave me has a type about this method with X ,c and others.So in my case .We choose some numbers like 1103515245.How this choice made? maybe depends on computer?
Yep, it generates 'pseudo random numbers'; the choice of these funny numbers
is crucial to the quality of these pseudo random numbers. Here is a nice link.
And no, those numbers are hardly related to the computer you use.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: HUNGER |
last post by:
HI,
I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long
plz help...
|
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...
|
by: Ben Justice |
last post by:
For a program in c, I need some random numbers for a system were people are
placing bets. This is not a commerical project btw. Generally, I tend to
rely on things from the standard library,...
|
by: Profetas |
last post by:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks
|
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 =...
|
by: Fernando Barsoba |
last post by:
Hi all,
I think this has been posted many times.. but I tried several recipes
for obtaining a random number between , but I can't make them work.
So, does anyone know how to generate random...
|
by: Frank Silvermann |
last post by:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MIN_WORD_LENGTH 9
|
by: ds |
last post by:
Hi all,
rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
| |