Questions tagged [srand]

a function which initializes the pseudo-random number generator in C/C++ or PHP

A random seed is used to determine the initial state of a pseudo-random number generator. Such generators are used in many programming languages.

For a specific generator and a single value of seed the sequence of generated numbers will be the same.

Sample code:

#include <cstdlib>

int seed = 111;
int arr1[10];
int arr2[10];
srand (seed);
for (int a=0; a<10; a++) {
  arr1[a] = rand();
}
srand (seed);
for (int a=0; a<10; a++) {
  arr2[a] = rand();
}
for (int a=0; a<10; a++) {
  assert arr1[a] == arr2[a];
}
396 questions
3
votes
1 answer

Call srand only once when loading perl modules

My script is separated into multiple packages. main.pl loads A.pm, which loads A::B.pm and A::C.pm. They all use rand. However, if I specify a seed for srand in main.pl, the latter modules seem to generate their own seed anyway. How can I seed the…
Anna
  • 2,645
  • 5
  • 25
  • 34
3
votes
1 answer

How to initialize random without time

I am programming a microcontroller (PSoC5), which doesn't have a system time. What is the best way to generate a seed to initialize srand? TIA
Philip F.
  • 1,167
  • 1
  • 18
  • 33
3
votes
1 answer

How do I fix compiler warnings C6386 and C6385?

For the program I am writing I need to create an array with a user-defined size, as well as random values between -15 and 15. As such i am using srand along with dynamic array casting. This is my full code below: #include #include…
J. Kent
  • 35
  • 1
  • 6
3
votes
4 answers

How can i generate random doubles in C?

I have to involve experimental data in my code by addressing a method for automated generation of non-trivial input test data.How can I do this,considering the fact that I also have to take into consideration numbers of double type? Here's some…
Neri-kun
  • 173
  • 3
  • 14
3
votes
1 answer

Why does my srand(time(NULL)) function generate the same number every time in c?

So I was creating a program that would call a function and return 0 or 1 (0 meaning tails and 1 meaning heads) and then use that to print the outcome of 100 flips. It seemed simple enough thinking I could use srand(time(NULL)) to seed rand() with…
Ton
  • 49
  • 1
  • 3
3
votes
4 answers

srand seed consistency between physical machines

I'm not quite sure how to phrase this question, but I couldn't find any others like it. Say I have this code: srand(1); srand(SOME_DEFINED_CONST_INT); If I run this executable on a number of different physical machines, is the sequence of rand()…
badgerr
  • 7,802
  • 2
  • 28
  • 43
3
votes
2 answers

srand function in class

I am learning C++ and I can't seem to find an answer to my issue. When I run my code, I don't get any compiler errors, but I when I call a function "getVin()" (supposed to generate a random number with the use of "generate()" function) it doesn't do…
LCKDWN
  • 31
  • 5
3
votes
3 answers

srand((unsigned)(time(NULL))); (rand())/(RAND_MAX/2) - 1 C# equivalent

What is the c# equivalent of the following c++: srand((unsigned)(time(NULL))); weight=(double)(rand())/(RAND_MAX/2) - 1;
purplelightspark
3
votes
1 answer

Unique random - srand in java ? (like in c++)

Currently I'm using a Random rand = new Random(); myNumber = rand.nextInt((100-0)+1)+0; and it's a pseudo-random, because I always get the same sequence of numbers. I remember in c++ you could just use the ctime and use srand to make it unique.…
Dawid
  • 585
  • 5
  • 26
3
votes
3 answers

Using C++ rand() to get random directions (up/down/left/right) - always getting up

I'm making a C++ text-based Battleship game. I use the rand() function to randomly place the computer's ships. I seed the number generator once at the beginning of main(), with the line below: srand(static_cast(time(0))); I later use the…
user3152847
3
votes
2 answers

C: srand does not influence random number generator

I have the following c code: #include #include #include int main(int argc, char *argv[]) { srand(time(NULL)); printf("%d\n", (int)random()); return 0; } To my understanding, this should print a different…
3
votes
4 answers

create a random sequence, skip to any part of the sequence

In Linux. There is an srand() function, where you supply a seed and it will guarantee the same sequence of pseudorandom numbers in subsequent calls to the random() function afterwards. Lets say, I want to store this pseudo random sequence by…
Michael Xu
  • 557
  • 1
  • 5
  • 14
3
votes
5 answers

Is it good idea to pass uninitialized variable to srand?

Is it good idea to pass uninitialized variable to srand instead of result of time(NULL)? It is one #include and one function call less. Example code: #include int main(void) { { usigned seed; //uninitialized …
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
3
votes
2 answers

Duplicate output when using srand for random seed

I'm using srand (time(NULL)); to generate a random seed. Problem is, I'm submitting 30+ identical jobs to a LINUX cluster. If I submit them one at a time, everything is fine, but of course I prefer to use a batch job to submit all 30 at once. Much…
3
votes
2 answers

Precedence rules - Trouble using brackets and division with srand to get a float number

I am trying to manipulate srand so that srand returns a decimal number by division. But it's not working. My Code doesn't return a decimal number even though precedence rules should prioritize brackets before division. #include #include…