okay, I know that the following will give me a number between 0 and 100; however, if I wanted to go from 0 to a number n, how would i do that ? Perhaps 44 or any number ?? Thanks

Code:srandom(time(0)); win=random()%100;

This is a discussion on *random number between 0 and n??* within the **C Programming** forums, part of the General Programming Boards category; okay, I know that the following will give me a number between 0 and 100; however, if I wanted to ...

- 02-12-2003 #1

- Join Date
- Sep 2002
- Posts
- 53

## random number between 0 and n??

okay, I know that the following will give me a number between 0 and 100; however, if I wanted to go from 0 to a number n, how would i do that ? Perhaps 44 or any number ?? Thanks

Code:srandom(time(0)); win=random()%100;

- 02-12-2003 #2
Same thing, but replace the literal with a variable :-)

Code:srandom(time(0)); win=random()%n;

*Cela*

- 02-12-2003 #3
Btw: I'm pretty sure

**win=random()%100;**would give you a number between 0 & 99, not 0 & 100. So in your function you may want to use (n+1) as the variable.

ie

Code:int n=44; srandom(time(0)); win=random()%(n+1); // Gives a random number between 0 & n (inclusive)

Demonographic rhinology is not the only possible outcome, but why take the chance

- 02-12-2003 #4

- Join Date
- Sep 2002
- Posts
- 53

## thanks...

Thank you.

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011