1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /*                                                                           */
3 /*                  This file is part of the program and library             */
4 /*         SCIP --- Solving Constraint Integer Programs                      */
5 /*                                                                           */
6 /*    Copyright (C) 2002-2021 Konrad-Zuse-Zentrum                            */
7 /*                            fuer Informationstechnik Berlin                */
8 /*                                                                           */
9 /*  SCIP is distributed under the terms of the ZIB Academic License.         */
10 /*                                                                           */
11 /*  You should have received a copy of the ZIB Academic License              */
12 /*  along with SCIP; see the file COPYING. If not visit scipopt.org.         */
13 /*                                                                           */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file   scip_randnumgen.c
17  * @ingroup OTHER_CFILES
18  * @brief  public methods for random numbers
19  * @author Tobias Achterberg
20  * @author Timo Berthold
21  * @author Gerald Gamrath
22  * @author Leona Gottwald
23  * @author Stefan Heinz
24  * @author Gregor Hendel
25  * @author Thorsten Koch
26  * @author Alexander Martin
27  * @author Marc Pfetsch
28  * @author Michael Winkler
29  * @author Kati Wolter
30  *
31  * @todo check all SCIP_STAGE_* switches, and include the new stages TRANSFORMED and INITSOLVE
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "scip/misc.h"
37 #include "scip/pub_message.h"
38 #include "scip/scip_mem.h"
39 #include "scip/scip_randnumgen.h"
40 #include "scip/set.h"
41 #include "scip/struct_scip.h"
42 
43 /** creates and initializes a random number generator
44  *
45  *  @note The initial seed is changed using SCIPinitializeRandomSeed()
46  */
SCIPcreateRandom(SCIP * scip,SCIP_RANDNUMGEN ** randnumgen,unsigned int initialseed,SCIP_Bool useglobalseed)47 SCIP_RETCODE SCIPcreateRandom(
48    SCIP*                 scip,               /**< SCIP data structure */
49    SCIP_RANDNUMGEN**     randnumgen,         /**< random number generator */
50    unsigned int          initialseed,        /**< initial random seed */
51    SCIP_Bool             useglobalseed       /**< should the supplied seed be initialized by SCIP's global seed shift? */
52    )
53 {
54    unsigned int modifiedseed;
55 
56    assert(scip != NULL);
57    assert(randnumgen != NULL);
58 
59    if( useglobalseed )
60       modifiedseed = SCIPinitializeRandomSeed(scip, initialseed);
61    else
62       modifiedseed = initialseed;
63 
64    SCIP_CALL( SCIPrandomCreate(randnumgen, SCIPblkmem(scip), modifiedseed) );
65 
66    return SCIP_OKAY;
67 }
68 
69 /** frees a random number generator */
SCIPfreeRandom(SCIP * scip,SCIP_RANDNUMGEN ** randnumgen)70 void SCIPfreeRandom(
71    SCIP*                 scip,               /**< SCIP data structure */
72    SCIP_RANDNUMGEN**     randnumgen          /**< random number generator */
73    )
74 {
75    assert(scip != NULL);
76    assert(randnumgen != NULL);
77 
78    SCIPrandomFree(randnumgen, SCIPblkmem(scip));
79 }
80 
81 /** initializes a random number generator with a given start seed
82  *
83  *  @note The seed is changed using SCIPinitializeRandomSeed()
84  */
SCIPsetRandomSeed(SCIP * scip,SCIP_RANDNUMGEN * randnumgen,unsigned int seed)85 void SCIPsetRandomSeed(
86    SCIP*                 scip,               /**< SCIP data structure */
87    SCIP_RANDNUMGEN*      randnumgen,         /**< random number generator */
88    unsigned int          seed                /**< new random seed */
89    )
90 {
91    unsigned int modifiedseed;
92 
93    assert(scip != NULL);
94    assert(randnumgen != NULL);
95 
96    modifiedseed = SCIPinitializeRandomSeed(scip, seed);
97 
98    SCIPrandomSetSeed(randnumgen, modifiedseed);
99 }
100 
101 /** modifies an initial seed value with the global shift of random seeds */
SCIPinitializeRandomSeed(SCIP * scip,unsigned int initialseedvalue)102 unsigned int SCIPinitializeRandomSeed(
103    SCIP*                 scip,               /**< SCIP data structure */
104    unsigned int          initialseedvalue    /**< initial seed value to be modified */
105    )
106 {
107    assert(scip != NULL);
108 
109    return SCIPsetInitializeRandomSeed(scip->set, initialseedvalue);
110 }
111