Generating random numbers with rand()

Last update on October 3, 2009 07:57 AM by jak58
Published by netty5

Generating random numbers with rand()








You may have noticed when using the rand() found in the standard library of C language, you often get unsatisfied by the results, they look the same.

For example, when trying 5 random numbers in a row:

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int i;
    for(i=0; i<5; i++)
    {
        printf("%d\n", rand());
    }
    return 0;
}


Execute the program and examine the results generated:

41
18467
6334
26500
19169


The outputs are significantly different from each other. But if you run your program once more, you will have the same set of numbers.

To change the way you’re random number generator proceed, you can modify a variable on which it relies for its calculations. We call it a seed.

This seed will change with the function srand():

srand(value of seed)


It needs a number that varies from time to time and that can not be predicted easily.
For example, you can take the number of cycles used by the processor since the start.
It can be obtained on x86 processors (Intel, AMD), with the assembly command rdtsc.
Writing a rdtsc() function calling this assembly command will definitely make your life easier, the syntax below works with gcc under Linux and can be found it with dev C++ under Windows.


#include <stdlib.h>
#include <stdio.h>

int rdtsc()
{
    __asm__ __volatile__("rdtsc");
}

int main()
{
    int i;
    for(i=0; i<5; i++)
    {
        srand(rdtsc());
        printf("%d\n", rand());
    }
    return 0;
}


With this code, you will generate random numbers more effectively.


Note:
But be aware this solution works only on x86 processors. If your program is to be platform independent, better find another solution.

Better avoid also enable optimizations features in the compiler (option-O1,-O2-O3 etc ...), when using the rdtsc.
Best answers for « Generating random numbers with rand() » in :
Download Random Number Generator Pro Show Random Number Generator Pro is a tool that generates a list of random numbers based on customizable criteria. You can choose the minimum and maximum limits and incrementing numbers. Limits can be positive or negative values. Advantage The...
Download Random Number Generator Pro Show Random Number Generator Pro is a very simple tool to generate a random number list. There are several combinations and you can define the randomization criteria. You simply select the lower and upper limits as well as the number increments and the...
How to change XP Serial/License number ShowHow to change XP Serial/License number Go to / Start Menu/ Run/ Type regedit and click OK. Before any operation in the registry, he should make a backup of the registry as a precaution In the Registry Editor, click File...
SERIAL NUMBER- Trend Micro PC CILLIN ShowSERIAL NUMBER- Trend Micro PC CILLIN Explanation How to replace your old serial number by the new one? Explanation Go to registry by Menu Start > Run and type REGEDIT and click on “OK”. In the open window under « My computer...
[C language]Checking whether an integer is a prime number Show[C language]Checking whether an integer is a prime number Definition of a prime number Algorithm 1: dividers between 2 and N-1 will be tested Algorithm 2: Even dividers will not be tested, research is limited to odd dividers Algorithm 3:...
Download Password Generator Professional 2009 ShowSave time when you need to generate a large number of passwords or license keys by using Password Generator Professional, a powerful application capable of creating strong passwords from random numbers. Advantage The program offers a truly...
Download USA Unlisted Cell Phone Numbers ShowHas you friend flown for America without telling you where he/she would settle? Dont panic! USA Unlisted Cell Phone Numbers is a program which can help you. By using this program, you simply need to know his/her phone number in order to find...
Download WIFI Key Generator ShowWifi Connections are certainly very practical, but given that they require no wiring, everybody can be connected to it if his computer is compatible with Wifi. WIFI Key Generator is a small application intended to generate random protection keys for...
Random access memory (RAM or PC memory) ShowTypes of random access memory There are generally two broad categories of random access memory: DRAM memories (Dynamic Random Access Module), which are inexpensive. They are used essentially for the computer's main memory SRAM memories (Static...
Representation of real numbers and integers ShowRepresenting a number in a computer Representing (or encoding) a number means to express it in binary form. Representing numbers in a computer is necessary in order for it to be able to store and manipulate them. However, the problem is that a...
IP Address ShowWhat is an IP address? Computers communicate over the Internet using the IP protocol (Internet Protocol), which uses numerical addresses, called IP addresses, made up of four whole numbers (4 bytes) between 0 and 255 and written in the format...