Sponsored Content
Full Discussion: Random number is not reused
Top Forums Programming Random number is not reused Post 302375993 by limmer on Monday 30th of November 2009 10:08:45 AM
Old 11-30-2009
Quote:
Originally Posted by pludi
How did you try to save it? Usually, I'd just allocate one global int, assign it the value, and be done with it. gcc shouldn't complain about that.
gcc error:
Quote:
23: error: initializer element is not constant
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>

int RANDOM(void)
{
    int RANDOMNUMBER = 0;

    srand((unsigned int)time(NULL));
    RANDOMNUMBER = rand() % 99999; // create a random 5 digit number to be reused in other functions

    if (RANDOMNUMBER < 10000)
    {
        RANDOMNUMBER = RANDOMNUMBER + 10000; // correction because sometimes the number generated is 4 digits long
    }

    return RANDOMNUMBER;
}

int NUMBER = RANDOM();

char *ONE(void)
{
    static char ONE1[15] = "";

    sprintf(ONE1, "ONE%.5i", NUMBER);

    return ONE1;
}

char *TWO(void)
{
    static char TWO2[15] = "";

    sprintf(TWO2, "TWO%.5i", NUMBER);

    return TWO2;
}

void THREE(void)
{
    printf("THREE%.5i\n", NUMBER);
}

int main(void)
{
/* A different random number is output every second. This is a problem because the program takes various seconds to execute.
How can I reuse a random number every time the program is executed ? */

    puts(ONE());
    sleep(1);
    puts(ONE());
    sleep(1);
    puts(ONE());
    sleep(1);
    puts(TWO());
    sleep(1);
    puts(TWO());
    sleep(1);
    puts(TWO());
    sleep(1);
    THREE();
    sleep(1);
    THREE();
    sleep(1);
    THREE();

    return 0;
}

 

10 More Discussions You Might Find Interesting

1. Programming

How to generate a random number?

How to generate a random integer with specific range(for example, from 1 to 1000)? Also, how to convert a floating point number into a integer? (2 Replies)
Discussion started by: MacMonster
2 Replies

2. Programming

random number logic -- issue

I use standard C random number generation logic in my application. long nCounter; long lRndNo; char rand; srand48(nCounter); lRndNo = lrand48(); sprintf(rand,"%010.10d",lRndNo); However we always find that the rand generated starts with '0','1' or '2'. I could not logically prove if... (1 Reply)
Discussion started by: asutoshch
1 Replies

3. Programming

Random number generation

Hi...How can I generate random numbers between a given range ...preferably between 1 and 100...in UNIX C programming...? I tried out functions like rand(),drand48() etc but didnt get satisfactory results... Thanks a lot in advance.......... (1 Reply)
Discussion started by: tej.buch
1 Replies

4. Shell Programming and Scripting

Regarding Random Number Genration

HI please help me in solving this issue. I have a shell script which consists of ten statemnts ( i mean 10 executable statments)...........and if i run that script the 10 statmenst will execute continously ............ But now my probelm is i have to write another script --> to... (0 Replies)
Discussion started by: lalitka
0 Replies

5. Shell Programming and Scripting

ksh: random number between 1-10

How do I create a random number between 1 and 10 in kornshell? (2 Replies)
Discussion started by: dangral
2 Replies

6. Programming

C Help; generating a random number.

Im new to C, and Im having a hard time getting a random number. In bash, I would do something similar to the following to get a random number; #!/bin/bash seed1=$RANDOM seed2=$RANDOM seed3=$RANDOM SEED=`expr $seed1 * $seed2 / $seed3` echo ${SEED%.*} Now, in online examples... (4 Replies)
Discussion started by: trey85stang
4 Replies

7. Programming

Generate random number

I saw this formula to generate random number between two specified values in shell script.the following. $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min)) Give a example in book. Generate random number between 6 and 30.like this. $(((RANDOM%30/3+1)*3)) But I have a... (1 Reply)
Discussion started by: luoluo
1 Replies

8. Shell Programming and Scripting

Random pieces of number

Hello folks, i have number for example 10 and i want to divide into 4 random pieces that may be (6+2+1+1). How can i do this via script i have random number 234951 and i want to divide into 31 pieces. (6 Replies)
Discussion started by: learnbash
6 Replies

9. Shell Programming and Scripting

Create random number

Hi, I'm trying to create a script that will print random numbers with length of three. Below is the expected out. 928-377-899 942-458-310 951-948-511 962-681-415 995-161-708 997-997-209 thanks (4 Replies)
Discussion started by: reignangel2003
4 Replies

10. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 Replies
RANDOM(3)						     Linux Programmer's Manual							 RANDOM(3)

NAME
random, srandom, initstate, setstate - random number generator SYNOPSIS
#include <stdlib.h> long int random(void); void srandom(unsigned int seed); char *initstate(unsigned int seed, char *state, size_t n); char *setstate(char *state); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): random(), srandom(), initstate(), setstate(): _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 DESCRIPTION
The random() function uses a nonlinear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to RAND_MAX. The period of this random number generator is very large, approx- imately 16 * ((2^31) - 1). The srandom() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by random(). These sequences are repeatable by calling srandom() with the same seed value. If no seed value is provided, the random() function is automati- cally seeded with a value of 1. The initstate() function allows a state array state to be initialized for use by random(). The size of the state array n is used by init- state() to decide how sophisticated a random number generator it should use -- the larger the state array, the better the random numbers will be. seed is the seed for the initialization, which specifies a starting point for the random number sequence, and provides for restarting at the same point. The setstate() function changes the state array used by the random() function. The state array state is used for random number generation until the next call to initstate() or setstate(). state must first have been initialized using initstate() or be the result of a previous call of setstate(). RETURN VALUE
The random() function returns a value between 0 and RAND_MAX. The srandom() function returns no value. The initstate() and setstate() functions return a pointer to the previous state array, or NULL on error. ERRORS
EINVAL A state array of less than 8 bytes was specified to initstate(). CONFORMING TO
4.3BSD, POSIX.1-2001. NOTES
Current "optimal" values for the size of the state array n are 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to the nearest known amount. Using less than 8 bytes will cause an error. This function should not be used in cases where multiple threads use random() and the behavior should be reproducible. Use random_r(3) for that purpose. Random-number generation is a complex topic. Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.) provides an excellent discussion of practi- cal random-number generation issues in Chapter 7 (Random Numbers). For a more theoretical discussion which also covers many practical issues in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981. SEE ALSO
drand48(3), rand(3), random_r(3), srand(3) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2009-02-03 RANDOM(3)
All times are GMT -4. The time now is 02:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy