473,386 Members | 1,699 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 seed in C (explain a code)

82
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
Aug 8 '07 #1
4 6594
JosAH
11,448 Expert 8TB
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
Aug 8 '07 #2
kalar
82
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?
Aug 8 '07 #3
JosAH
11,448 Expert 8TB
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
Aug 8 '07 #4
kalar
82
Thank you
very much
Aug 8 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
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...
36
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,...
36
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
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 =...
11
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...
10
by: Frank Silvermann | last post by:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define MIN_WORD_LENGTH 9
5
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...
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: 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,...
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: 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...
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?
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
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
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,...
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.