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
6
votes
1 answer

Not able to use srand48() after changing to c++ 11

Why am I not able to compile my code to c++ 11 and use the srand48 function? I have a program where I play around with some matrices. The problem is that when I compile the code with the -std=c++0x flag. I want to use some c++11 only functions and…
6
votes
3 answers

random numbers and multiple srand calls

I'm writing a program that will generate numerous random numbers in loops. I'm trying to make the numbers somewhat less predictable (not only for security, but to avoid collisions on multiple threads). I've noticed that many documents recommended…
cegfault
  • 6,442
  • 3
  • 27
  • 49
6
votes
2 answers

How do I seed the rand() function in Objective-C?

Part of what I'm developing is a random company name generator. It draws from several arrays of name parts. I use the rand() function to draw the random name parts. However, the same "random" numbers are always generated in the same sequence every…
inorganik
  • 24,255
  • 17
  • 90
  • 114
6
votes
4 answers

rand() %4000000000UL giving only small values

I have a question about the following code : #include #include int main(){ unsigned long int blob; srand(time(0)); for(int counter = 0; counter <= 100; counter++) { blob = rand() % 4000000000UL ; …
Jane Doe
  • 63
  • 3
5
votes
2 answers

Shouldn't the same srand value produce the same random numbers?

When I repeatedly run this code, srand 1; my @x = (1..1000).pick: 100; say sum @x; I get different answers each time. If I'm resetting with srand why shouldn't it produce the same random numbers each time? The error occurs in the REPL. The error…
Jim Bollinger
  • 1,671
  • 1
  • 13
5
votes
1 answer

Random numbers in POSIX C API

I'm looking to generate large, non-negative integer random values on a POSIX system. I've found 2 possible functions that fit the bill, and their respective initializers: #include long int random(void); void…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
5
votes
3 answers

C++ rand () not providing correct number based on seed

I am working on a C++ assignment for my class, and we have to use rand () with a seed of 99 to produce a set of values. However, my problem is that when I try to create a value within our parameters, the number is different than what the instructor…
5
votes
6 answers

Repeatable randomness in Ruby

I know I can "restart" my rand calls by calling srand with a seed, but surely this would affect future calls to rand by other library methods, including cryptographic methods? How can I repeat my randomness in certain places and still ensure that…
Gareth
  • 133,157
  • 36
  • 148
  • 157
5
votes
5 answers

Why does rand() produce the same value when seeded with 1 and UINT_MAX?

Here's some code: #include int main() { srand(1); std::cout << rand() << "\n"; srand(UINT_MAX); std::cout << rand() << "\n"; } This produces the following output: 16807 16807 Why do these two seeds produce the same…
Luke
  • 7,110
  • 6
  • 45
  • 74
5
votes
1 answer

Generate big numbers with srand() in c

I want to genrerate big random numbers in c. The problem is that the biggest number srand() can generate is about 37000. I want to create a number in the intervall 70000 to 2150000000. Could anyone help me with this. Random number…
Isak
  • 115
  • 1
  • 2
  • 9
5
votes
2 answers

c++ srand does not give same sequences of random numbers

I have a optimisation algorithm which uses rand() and srand(). In order to be able to test the behaviour I have set the seed to a specific number in order to get the same sequence of random numbers on different runs of the program. #define RN…
Cristina
  • 51
  • 5
5
votes
1 answer

C++ random_shuffle() how does it work?

I have a Deck vector with 52 Card, and I want to shuffle it. vector cards; So I used this: random_shuffle(cards.begin(), cards.end()); The problem was that it gave me the same result every time, so I used srand to randomize…
Viktor
  • 63
  • 1
  • 4
5
votes
5 answers

C++ random number from a set

Is it possible to print a random number in C++ from a set of numbers with ONE SINGLE statement? Let's say the set is {2, 5, 22, 55, 332} I looked up rand() but I doubt it's possible to do in a single statement.
user69514
  • 26,935
  • 59
  • 154
  • 188
5
votes
3 answers

Do I need '(unsigned int)' before 'time(null)' in the srand function in c?

I've seen some guide on generating random numbers with C: two things got me wondering: it is said that in addition to stdlib.h and time.h libraries I have to include the math.h library for it to work, why? (afaik the srand and rand functions are…
Medvednic
  • 692
  • 3
  • 11
  • 28
5
votes
1 answer

What are the weaknesses of Perl's srand() default seed, post version 5.004?

I can find plenty of documentation as to issues with use of time() prior to Perl version 5.004, but nothing following. For a homework assignment, we are asked to reverse-engineer a program's results based on the assumption that the default Perl…
Dan
  • 3,246
  • 1
  • 32
  • 52
1 2
3
26 27