Jump to content

Need help with rand c++

- - - - -

  • Please log in to reply
5 replies to this topic

#1
snorifu

snorifu

    Newbie

  • Members
  • Pip
  • 7 posts
Hello,
I need to use the rand() function for my program. But it isn't working the way I want.
I think the code is now generating a number between the number choice[8] contains and a number choice[0] contains. But I want to get a number amongst these :1,2,3,5,6,7,8,9. Does anyone has a solution for this?
I'm not very good in English so please reply if you don't understand me.
int choice[8]={1,2,3,5,6,7,8,9};

srand(time (NULL));

number[0]=rand() % choice[8] + choice[0]; 


#2
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
i don't quite understand what you are trying to do, but you can randomize some int form 1-9 with:
yours_int = rand() % 9 + 1;

int choice[8];
is a 8 element table. To randomize it you shall use 'for' loop.
This is working for a windows.
#include <cstdlib>

#include <iostream>

#include <time.h>

using namespace std;


int main(int argc, char *argv[])

{

  int choice[8]; 

  srand ( time(NULL) );

  for (int i=0;i<8;i++)

   {choice[i]=  rand() % 9 + 1;

    cout <<endl << choice[i]; 

} 



    system("PAUSE");

    return EXIT_SUCCESS;

}


#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

some_random_number = rand() % lower_bound + upper_bound;

You can set ranges with that formula. So, if you'd want random numbers between [5, 15] then you'd say:

random_number = rand() % 11 + 5;

Since modulo operator ( % ) has higher precedence than addition, it will return a number between 0 and 10. Then'll add 5 and we get numbers in [5, 15] range.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
snorifu

snorifu

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks for your reply's but it's not what I mean.
I need to get a random numberout of an array. So the array could be {1,2,3,5,6,7,8,9}
but it could also be {1,4,7,2,9,6,5,4}. Now my question is is there a in built function in c++ or c that does that?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You need to generate a random INDEX for the array, and then use that to access the element of interest, not generate it based on the values in fixed locations in the array.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
snorifu

snorifu

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks WingedPanther! That is exactly the solution I wanted:)
here is my code:
srand(time (NULL));

value=rand() % max + 0; 

number[0]=choice[value];

Edited by snorifu, 11 April 2011 - 12:45 AM.
forgot a semicolon





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users